Skip to content

Commit aa67dea

Browse files
committed
chore: add avalonia DataValidationErrors demo.
1 parent 5de152b commit aa67dea

File tree

8 files changed

+246
-162
lines changed

8 files changed

+246
-162
lines changed

src/Avalonia/HandyControlDemo_Avalonia/App.axaml.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Globalization;
2+
using System.Linq;
23
using Avalonia;
34
using Avalonia.Controls.ApplicationLifetimes;
45
using Avalonia.Data.Core.Plugins;
@@ -17,7 +18,7 @@ public override void Initialize()
1718
public override void OnFrameworkInitializationCompleted()
1819
{
1920
Properties.Langs.Lang.Culture = new CultureInfo("en");
20-
BindingPlugins.DataValidators.RemoveAt(0);
21+
RemoveDefaultDataValidator();
2122

2223
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
2324
{
@@ -26,4 +27,17 @@ public override void OnFrameworkInitializationCompleted()
2627

2728
base.OnFrameworkInitializationCompleted();
2829
}
30+
31+
private static void RemoveDefaultDataValidator()
32+
{
33+
// Get an array of plugins to remove
34+
var dataValidationPluginsToRemove =
35+
BindingPlugins.DataValidators.OfType<DataAnnotationsValidationPlugin>().ToArray();
36+
37+
// remove each entry found
38+
foreach (var plugin in dataValidationPluginsToRemove)
39+
{
40+
BindingPlugins.DataValidators.Remove(plugin);
41+
}
42+
}
2943
}

src/Avalonia/HandyControlDemo_Avalonia/UserControl/Styles/TextBoxDemo.axaml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
<UserControl xmlns="https://github.com/avaloniaui"
22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3-
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
4-
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
53
xmlns:hc="https://handyorg.github.io/handycontrol"
6-
mc:Ignorable="d"
7-
d:DesignWidth="800"
8-
d:DesignHeight="450"
9-
x:Class="HandyControlDemo.UserControl.TextBoxDemo">
4+
xmlns:vm="clr-namespace:HandyControlDemo.ViewModel"
5+
x:Class="HandyControlDemo.UserControl.TextBoxDemo"
6+
DataContext="{Binding InputElementDemo, Source={x:Static vm:ViewModelLocator.Instance}}">
107
<ScrollViewer>
118
<hc:UniformSpacingPanel Spacing="32"
129
Margin="32"
@@ -32,6 +29,7 @@
3229
Text="This is the content"
3330
IsEnabled="False" />
3431
<TextBox Watermark="please enter email"
32+
Text="{Binding Email1, UpdateSourceTrigger=PropertyChanged}"
3533
hc:TitleElement.Title="This item must be filled in"
3634
Theme="{StaticResource TextBoxExtend}"
3735
hc:InfoElement.Necessary="True"
@@ -44,6 +42,7 @@
4442
Margin="0,32,0,0"
4543
Text="This is the content" />
4644
<TextBox Width="380"
45+
Text="{Binding Text1, UpdateSourceTrigger=PropertyChanged}"
4746
hc:TitleElement.TitleWidth="120"
4847
Watermark="Please enter content"
4948
hc:TitleElement.TitlePlacement="Left"
@@ -75,6 +74,7 @@
7574
Text="This is the content"
7675
IsEnabled="False" />
7776
<TextBox Watermark="please enter email"
77+
Text="{Binding Email2, UpdateSourceTrigger=PropertyChanged}"
7878
hc:TitleElement.Title="This item must be filled in"
7979
Theme="{StaticResource TextBoxExtend.Small}"
8080
hc:InfoElement.Necessary="True"
@@ -87,6 +87,7 @@
8787
Margin="0,32,0,0"
8888
Text="This is the content" />
8989
<TextBox Width="380"
90+
Text="{Binding Text2, UpdateSourceTrigger=PropertyChanged}"
9091
hc:TitleElement.TitleWidth="120"
9192
Watermark="Please enter content"
9293
hc:TitleElement.TitlePlacement="Left"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.ComponentModel.DataAnnotations;
2+
using CommunityToolkit.Mvvm.ComponentModel;
3+
4+
namespace HandyControlDemo.ViewModel;
5+
6+
public partial class InputElementDemoViewModel : ObservableValidator
7+
{
8+
[Required] [EmailAddress] [ObservableProperty] [NotifyDataErrorInfo]
9+
private string? _email1;
10+
11+
[Required] [EmailAddress] [ObservableProperty] [NotifyDataErrorInfo]
12+
private string? _email2;
13+
14+
[Required] [ObservableProperty] [NotifyDataErrorInfo]
15+
private string? _text1;
16+
17+
[Required] [ObservableProperty] [NotifyDataErrorInfo]
18+
private string? _text2;
19+
}

src/Avalonia/HandyControlDemo_Avalonia/ViewModel/DemoViewModelBase`1.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@
33

44
namespace HandyControlDemo.ViewModel;
55

6-
public class DemoViewModelBase<T> : ObservableObject
6+
public partial class DemoViewModelBase<T> : ObservableObject
77
{
8-
private IList<T> _dataList = [];
9-
10-
public IList<T> DataList
11-
{
12-
get => _dataList;
13-
set => SetProperty(ref _dataList, value);
14-
}
8+
[ObservableProperty] private IList<T> _dataList = [];
159
}

src/Avalonia/HandyControlDemo_Avalonia/ViewModel/ViewModelLocator.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ private ViewModelLocator()
1919

2020
services.AddSingleton<DataService>();
2121
services.AddTransient<MainViewModel>();
22+
services.AddTransient<InputElementDemoViewModel>();
2223

2324
_serviceProvider = services.BuildServiceProvider();
2425
}
2526

2627
public MainViewModel Main => _serviceProvider.GetService<MainViewModel>()!;
28+
29+
public InputElementDemoViewModel InputElementDemo => _serviceProvider.GetService<InputElementDemoViewModel>()!;
2730
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<ResourceDictionary xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
3+
<ControlTheme x:Key="{x:Type DataValidationErrors}"
4+
TargetType="DataValidationErrors">
5+
<Setter Property="Template">
6+
<ControlTemplate>
7+
<DockPanel LastChildFill="True">
8+
<ContentControl x:DataType="DataValidationErrors"
9+
Content="{Binding (DataValidationErrors.Errors)}"
10+
ContentTemplate="{TemplateBinding ErrorTemplate}"
11+
DataContext="{TemplateBinding Owner}"
12+
DockPanel.Dock="Right"
13+
IsVisible="{Binding (DataValidationErrors.HasErrors)}" />
14+
<ContentPresenter Name="PART_ContentPresenter"
15+
Padding="{TemplateBinding Padding}"
16+
Background="{TemplateBinding Background}"
17+
BorderBrush="{TemplateBinding BorderBrush}"
18+
BorderThickness="{TemplateBinding BorderThickness}"
19+
Content="{TemplateBinding Content}"
20+
ContentTemplate="{TemplateBinding ContentTemplate}"
21+
CornerRadius="{TemplateBinding CornerRadius}" />
22+
</DockPanel>
23+
</ControlTemplate>
24+
</Setter>
25+
<Setter Property="ErrorTemplate">
26+
<DataTemplate>
27+
<Canvas Width="16"
28+
Height="16"
29+
Margin="4,0,1,0"
30+
Background="Transparent">
31+
<Canvas.Styles>
32+
<Style Selector="ToolTip">
33+
<Setter Property="BorderBrush"
34+
Value="{DynamicResource DangerBrush}" />
35+
</Style>
36+
</Canvas.Styles>
37+
<ToolTip.Tip>
38+
<ItemsControl x:DataType="DataValidationErrors"
39+
ItemsSource="{Binding}" />
40+
</ToolTip.Tip>
41+
<Path Data="{StaticResource WarningGeometry}"
42+
Width="16"
43+
Height="16"
44+
Fill="{DynamicResource DangerBrush}"
45+
Stretch="Uniform" />
46+
</Canvas>
47+
</DataTemplate>
48+
</Setter>
49+
</ControlTheme>
50+
</ResourceDictionary>

0 commit comments

Comments
 (0)