Browse Source
* Introduce ErrorConverter and DisplayErrors attached properties - the converter can be used to change the way a message is print - we use DisplayErrors to get the converted error messages * Adjust FluentTheme * [WIP] Add a sample Page for DataValidationErrors * use a private attached property to store recent errors this approach gets rid of the need to DisplayErrors property * Update samples with some additional details * Reuse rich SetError logic in DataGrid as well * Unify some code with OnErrorsOrConverterChanged * Restore old behavior with null default value * Add SetErrorConverter test --------- Co-authored-by: Max Katz <maxkatz6@outlook.com>UnsafeAsSpan-logical-children
committed by
GitHub
7 changed files with 208 additions and 28 deletions
@ -0,0 +1,42 @@ |
|||
<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 TextBlock.TextWrapping="Wrap"> |
|||
<Label Target="Email1" Content="Email validation" /> |
|||
<TextBox x:Name="Email1" |
|||
MaxWidth="400" |
|||
HorizontalAlignment="Left" |
|||
Text="{Binding DataAnnotationsSample}" /> |
|||
|
|||
<Label Target="Email2" Content="Email validation with custom error converter" |
|||
Margin="0, 10, 0, 0" /> |
|||
<TextBox x:Name="Email2" |
|||
Text="{Binding DataAnnotationsSample}" |
|||
MaxWidth="400" |
|||
HorizontalAlignment="Left" |
|||
DataValidationErrors.ErrorConverter="{Binding Converter}" /> |
|||
|
|||
<Label Target="SetterException" Content="Setter exception with custom error converter and tooltip error" |
|||
Margin="0, 10, 0, 0" /> |
|||
<TextBox x:Name="SetterException" |
|||
Text="{Binding ExceptionInsideSetterSample}" |
|||
MaxWidth="400" |
|||
HorizontalAlignment="Left" |
|||
DataValidationErrors.ErrorConverter="{Binding ExceptionConverter}"> |
|||
<TextBox.Styles> |
|||
<Style Selector="DataValidationErrors"> |
|||
<Setter Property="Theme" Value="{DynamicResource TooltipDataValidationErrors}" /> |
|||
</Style> |
|||
</TextBox.Styles> |
|||
</TextBox> |
|||
</StackPanel> |
|||
</UserControl> |
|||
|
|||
@ -0,0 +1,14 @@ |
|||
using Avalonia; |
|||
using Avalonia.Controls; |
|||
using Avalonia.Markup.Xaml; |
|||
|
|||
namespace ControlCatalog.Pages; |
|||
|
|||
public partial class DataValidationPage : UserControl |
|||
{ |
|||
public DataValidationPage() |
|||
{ |
|||
InitializeComponent(); |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,45 @@ |
|||
using System; |
|||
using System.ComponentModel; |
|||
using System.ComponentModel.DataAnnotations; |
|||
using MiniMvvm; |
|||
|
|||
namespace ControlCatalog.ViewModels; |
|||
|
|||
public class DataValidationViewModel : ViewModelBase |
|||
{ |
|||
private string? _DataAnnotationsSample; |
|||
|
|||
[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; |
|||
|
|||
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!"; |
|||
}); |
|||
} |
|||
Loading…
Reference in new issue