Skip to content

Commit

Permalink
[WIP] Add a sample Page for DataValidationErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
timunie committed May 10, 2023
1 parent 4487cb4 commit 228e7c9
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
3 changes: 3 additions & 0 deletions samples/ControlCatalog/MainView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@
ScrollViewer.VerticalScrollBarVisibility="Disabled">
<pages:DataGridPage />
</TabItem>
<TabItem Header="Data Validation">
<pages:DataValidationPage />
</TabItem>
<TabItem Header="Date/Time Picker">
<pages:DateTimePickerPage />
</TabItem>
Expand Down
20 changes: 20 additions & 0 deletions samples/ControlCatalog/Pages/DataValidationPage.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="clr-namespace:ControlCatalog.ViewModels"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:DataType="viewModels:DataValidationViewModel"
x:Class="ControlCatalog.Pages.DataValidationPage">
<UserControl.DataContext>
<viewModels:DataValidationViewModel />
</UserControl.DataContext>
<StackPanel>
<TextBox Text="{Binding DataAnnotationsSample}"
DataValidationErrors.ErrorConverter="{Binding Converter}" />

<TextBox Text="{Binding ExceptionInsideSetterSample}"
DataValidationErrors.ErrorConverter="{Binding ExceptionConverter}" />
</StackPanel>
</UserControl>

19 changes: 19 additions & 0 deletions samples/ControlCatalog/Pages/DataValidationPage.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;

namespace ControlCatalog.Pages;

public partial class DataValidationPage : UserControl
{
public DataValidationPage()
{
InitializeComponent();
}

private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
}

50 changes: 50 additions & 0 deletions samples/ControlCatalog/ViewModels/DataValidationViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using MiniMvvm;

namespace ControlCatalog.ViewModels;

public class DataValidationViewModel : ViewModelBase
{
private string? _DataAnnotationsSample;

/// <summary>
/// Gets or sets an E-Mail-Address validated using annotations
/// </summary>
[Required]
[EmailAddress]
[MinLength(5)]
public string? DataAnnotationsSample
{
get => _DataAnnotationsSample;
set => RaiseAndSetIfChanged(ref _DataAnnotationsSample, value);
}

public Func<object, object> Converter { get; } = new Func<object, object>(o =>
{
return $"Error: {o}";
});


private string? _ExceptionInsideSetterSample;

/// <summary>
/// Gets or sets an E-Mail-Address validated using annotations
/// </summary>
public string? ExceptionInsideSetterSample
{
get => _ExceptionInsideSetterSample;
set
{
if (value is null || value.Length < 5) throw new ArgumentOutOfRangeException(nameof(value), "Give me 5 or more letter please :-)");

RaiseAndSetIfChanged(ref _ExceptionInsideSetterSample, value);
}
}

public Func<object, object> ExceptionConverter { get; } = new Func<object, object>(o =>
{
return o is Exception ex ? $"Huh, there was an Exception: {ex.Message}" : "Something went really wrong!";
});
}

0 comments on commit 228e7c9

Please sign in to comment.