-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Add a sample Page for DataValidationErrors
- Loading branch information
Showing
4 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
50
samples/ControlCatalog/ViewModels/DataValidationViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"; | ||
}); | ||
} |