chore: add avalonia DataValidationErrors demo.

This commit is contained in:
NaBian committed 2025-08-06 22:09:56 +08:00
1 parent 5de152b20e
commit aa67dea033
8 files changed
+246 -162

No files matched your search

@@ -1,4 +1,5 @@
using System.Globalization;
using System.Linq;
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Data.Core.Plugins;
@@ -17,7 +18,7 @@ public class App : Application
public override void OnFrameworkInitializationCompleted()
{
Properties.Langs.Lang.Culture = new CultureInfo("en");
BindingPlugins.DataValidators.RemoveAt(0);
RemoveDefaultDataValidator();
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
@@ -26,4 +27,17 @@ public class App : Application
base.OnFrameworkInitializationCompleted();
}
private static void RemoveDefaultDataValidator()
{
// Get an array of plugins to remove
var dataValidationPluginsToRemove =
BindingPlugins.DataValidators.OfType<DataAnnotationsValidationPlugin>().ToArray();
// remove each entry found
foreach (var plugin in dataValidationPluginsToRemove)
{
BindingPlugins.DataValidators.Remove(plugin);
}
}
}
@@ -1,12 +1,9 @@
<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:hc="https://handyorg.github.io/handycontrol"
mc:Ignorable="d"
d:DesignWidth="800"
d:DesignHeight="450"
x:Class="HandyControlDemo.UserControl.TextBoxDemo">
xmlns:vm="clr-namespace:HandyControlDemo.ViewModel"
x:Class="HandyControlDemo.UserControl.TextBoxDemo"
DataContext="{Binding InputElementDemo, Source={x:Static vm:ViewModelLocator.Instance}}">
<ScrollViewer>
<hc:UniformSpacingPanel Spacing="32"
Margin="32"
@@ -32,6 +29,7 @@
Text="This is the content"
IsEnabled="False" />
<TextBox Watermark="please enter email"
Text="{Binding Email1, UpdateSourceTrigger=PropertyChanged}"
hc:TitleElement.Title="This item must be filled in"
Theme="{StaticResource TextBoxExtend}"
hc:InfoElement.Necessary="True"
@@ -44,6 +42,7 @@
Margin="0,32,0,0"
Text="This is the content" />
<TextBox Width="380"
Text="{Binding Text1, UpdateSourceTrigger=PropertyChanged}"
hc:TitleElement.TitleWidth="120"
Watermark="Please enter content"
hc:TitleElement.TitlePlacement="Left"
@@ -75,6 +74,7 @@
Text="This is the content"
IsEnabled="False" />
<TextBox Watermark="please enter email"
Text="{Binding Email2, UpdateSourceTrigger=PropertyChanged}"
hc:TitleElement.Title="This item must be filled in"
Theme="{StaticResource TextBoxExtend.Small}"
hc:InfoElement.Necessary="True"
@@ -87,6 +87,7 @@
Margin="0,32,0,0"
Text="This is the content" />
<TextBox Width="380"
Text="{Binding Text2, UpdateSourceTrigger=PropertyChanged}"
hc:TitleElement.TitleWidth="120"
Watermark="Please enter content"
hc:TitleElement.TitlePlacement="Left"
@@ -0,0 +1,19 @@
using System.ComponentModel.DataAnnotations;
using CommunityToolkit.Mvvm.ComponentModel;
namespace HandyControlDemo.ViewModel;
public partial class InputElementDemoViewModel : ObservableValidator
{
[Required] [EmailAddress] [ObservableProperty] [NotifyDataErrorInfo]
private string? _email1;
[Required] [EmailAddress] [ObservableProperty] [NotifyDataErrorInfo]
private string? _email2;
[Required] [ObservableProperty] [NotifyDataErrorInfo]
private string? _text1;
[Required] [ObservableProperty] [NotifyDataErrorInfo]
private string? _text2;
}
@@ -3,13 +3,7 @@ using CommunityToolkit.Mvvm.ComponentModel;
namespace HandyControlDemo.ViewModel;
public class DemoViewModelBase<T> : ObservableObject
public partial class DemoViewModelBase<T> : ObservableObject
{
private IList<T> _dataList = [];
public IList<T> DataList
{
get => _dataList;
set => SetProperty(ref _dataList, value);
}
[ObservableProperty] private IList<T> _dataList = [];
}
@@ -19,9 +19,12 @@ public class ViewModelLocator
services.AddSingleton<DataService>();
services.AddTransient<MainViewModel>();
services.AddTransient<InputElementDemoViewModel>();
_serviceProvider = services.BuildServiceProvider();
}
public MainViewModel Main => _serviceProvider.GetService<MainViewModel>()!;
public InputElementDemoViewModel InputElementDemo => _serviceProvider.GetService<InputElementDemoViewModel>()!;
}