feat: add value validation.

This commit is contained in:
rabbitism
2024-01-25 20:07:51 +08:00
parent d1bb258b28
commit 2855f49165
4 changed files with 160 additions and 27 deletions

View File

@@ -1,19 +1,30 @@
<UserControl xmlns="https://github.com/avaloniaui" <UserControl
x:Class="Ursa.Demo.Pages.EnumSelectorDemo"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800"
xmlns:vm="clr-namespace:Ursa.Demo.ViewModels;assembly=Ursa.Demo"
xmlns:u="https://irihi.tech/ursa" xmlns:u="https://irihi.tech/ursa"
x:DataType="vm:EnumSelectorDemoViewModel" xmlns:vm="clr-namespace:Ursa.Demo.ViewModels;assembly=Ursa.Demo"
x:CompileBindings="True"
d:DesignHeight="450" d:DesignHeight="450"
x:Class="Ursa.Demo.Pages.EnumSelectorDemo"> d:DesignWidth="800"
x:CompileBindings="True"
x:DataType="vm:EnumSelectorDemoViewModel"
mc:Ignorable="d">
<StackPanel> <StackPanel>
<TextBlock Text="Select Type"></TextBlock> <ToggleSwitch Name="description" Content="Show Description" />
<ComboBox ItemsSource="{Binding Types}" DisplayMemberBinding="{Binding Name}" SelectedItem="{Binding SelectedType}"/> <TextBlock Text="Select Type" />
<TextBlock Text="Select Value"></TextBlock> <ComboBox
<u:EnumSelector EnumType="{Binding SelectedType}" Value="{Binding Value}" /> Width="200"
<TextBlock Text="{Binding Value}"></TextBlock> DisplayMemberBinding="{Binding Name}"
ItemsSource="{Binding Types}"
SelectedItem="{Binding SelectedType}" />
<TextBlock Text="Select Value" />
<u:EnumSelector
Width="200"
DisplayDescription="{Binding #description.IsChecked}"
EnumType="{Binding SelectedType}"
Value="{Binding Value}" />
<TextBlock Text="{Binding Value}" />
</StackPanel> </StackPanel>
</UserControl> </UserControl>

View File

@@ -1,5 +1,7 @@
using System; using System;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Input;
using Avalonia.Animation; using Avalonia.Animation;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Data; using Avalonia.Data;
@@ -7,6 +9,7 @@ using Avalonia.Input;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using Avalonia.Layout; using Avalonia.Layout;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
namespace Ursa.Demo.ViewModels; namespace Ursa.Demo.ViewModels;
@@ -50,6 +53,15 @@ public class EnumSelectorDemoViewModel: ObservableObject
typeof(Key), typeof(Key),
typeof(KeyModifiers), typeof(KeyModifiers),
typeof(RoutingStrategies), typeof(RoutingStrategies),
typeof(CustomEnum),
}; };
} }
} }
public enum CustomEnum
{
[Description("是")]
Yes,
[Description("否")]
No,
}

View File

@@ -4,10 +4,29 @@
xmlns:u="https://irihi.tech/ursa"> xmlns:u="https://irihi.tech/ursa">
<!-- Add Resources Here --> <!-- Add Resources Here -->
<ControlTheme x:Key="{x:Type u:EnumSelector}" TargetType="u:EnumSelector"> <ControlTheme x:Key="{x:Type u:EnumSelector}" TargetType="u:EnumSelector">
<Setter Property="HorizontalAlignment" Value="Left"></Setter>
<Setter Property="Template"> <Setter Property="Template">
<ControlTemplate TargetType="u:EnumSelector"> <ControlTemplate TargetType="u:EnumSelector">
<ComboBox ItemsSource="{TemplateBinding Values}" SelectedItem="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value, Mode=TwoWay}" /> <ComboBox
Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Bounds.Width}"
Name="PART_ComboBox"
ItemsSource="{TemplateBinding Values}"
SelectedItem="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=SelectedValue, Mode=TwoWay}" />
</ControlTemplate> </ControlTemplate>
</Setter> </Setter>
<Style Selector="^[DisplayDescription=True] /template/ ComboBox">
<Setter Property="ItemTemplate">
<DataTemplate x:DataType="u:EnumItemTuple">
<TextBlock Text="{Binding DisplayName}"></TextBlock>
</DataTemplate>
</Setter>
</Style>
<Style Selector="^[DisplayDescription=False] /template/ ComboBox">
<Setter Property="ItemTemplate">
<DataTemplate x:DataType="u:EnumItemTuple">
<TextBlock Text="{Binding Value}"></TextBlock>
</DataTemplate>
</Setter>
</Style>
</ControlTheme> </ControlTheme>
</ResourceDictionary> </ResourceDictionary>

View File

@@ -1,3 +1,4 @@
using System.ComponentModel;
using Avalonia; using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.Primitives; using Avalonia.Controls.Primitives;
@@ -5,6 +6,12 @@ using Avalonia.Data;
namespace Ursa.Controls; namespace Ursa.Controls;
public class EnumItemTuple
{
public string? DisplayName { get; set; }
public object? Value { get; set; }
}
public class EnumSelector: TemplatedControl public class EnumSelector: TemplatedControl
{ {
public static readonly StyledProperty<Type?> EnumTypeProperty = AvaloniaProperty.Register<EnumSelector, Type?>( public static readonly StyledProperty<Type?> EnumTypeProperty = AvaloniaProperty.Register<EnumSelector, Type?>(
@@ -23,7 +30,18 @@ public class EnumSelector: TemplatedControl
} }
public static readonly StyledProperty<object?> ValueProperty = AvaloniaProperty.Register<EnumSelector, object?>( public static readonly StyledProperty<object?> ValueProperty = AvaloniaProperty.Register<EnumSelector, object?>(
nameof(Value), defaultBindingMode: BindingMode.TwoWay); nameof(Value), defaultBindingMode: BindingMode.TwoWay, coerce:OnValueCoerce);
private static object? OnValueCoerce(AvaloniaObject o, object? value)
{
if (o is not EnumSelector selector) return null;
if (value is null) return null;
if (value.GetType() != selector.EnumType) return null;
var first = selector.Values.FirstOrDefault(a => Equals(a.Value, value));
if (first is null) return null;
return value;
}
public object? Value public object? Value
{ {
@@ -31,37 +49,110 @@ public class EnumSelector: TemplatedControl
set => SetValue(ValueProperty, value); set => SetValue(ValueProperty, value);
} }
public static readonly DirectProperty<EnumSelector, IList<object?>?> ValuesProperty = AvaloniaProperty.RegisterDirect<EnumSelector, IList<object?>?>( private EnumItemTuple? _selectedValue;
public static readonly DirectProperty<EnumSelector, EnumItemTuple?> SelectedValueProperty = AvaloniaProperty.RegisterDirect<EnumSelector, EnumItemTuple?>(
nameof(SelectedValue), o => o.SelectedValue, (o, v) => o.SelectedValue = v);
public EnumItemTuple? SelectedValue
{
get => _selectedValue;
private set => SetAndRaise(SelectedValueProperty, ref _selectedValue, value);
}
public static readonly DirectProperty<EnumSelector, IList<EnumItemTuple>?> ValuesProperty = AvaloniaProperty.RegisterDirect<EnumSelector, IList<EnumItemTuple>?>(
nameof(Values), o => o.Values); nameof(Values), o => o.Values);
private IList<object?>? _values; private IList<EnumItemTuple>? _values;
internal IList<object?>? Values internal IList<EnumItemTuple>? Values
{ {
get => _values; get => _values;
private set => SetAndRaise(ValuesProperty, ref _values, value); private set => SetAndRaise(ValuesProperty, ref _values, value);
} }
public static readonly StyledProperty<bool> DisplayDescriptionProperty = AvaloniaProperty.Register<EnumSelector, bool>(
nameof(DisplayDescription));
public bool DisplayDescription
{
get => GetValue(DisplayDescriptionProperty);
set => SetValue(DisplayDescriptionProperty, value);
}
static EnumSelector() static EnumSelector()
{ {
EnumTypeProperty.Changed.AddClassHandler<EnumSelector, Type?>((o, e) => o.OnTypeChanged(e)); EnumTypeProperty.Changed.AddClassHandler<EnumSelector, Type?>((o, e) => o.OnTypeChanged(e));
SelectedValueProperty.Changed.AddClassHandler<EnumSelector, EnumItemTuple?>((o, e) => o.OnSelectedValueChanged(e));
ValueProperty.Changed.AddClassHandler<EnumSelector, object?>((o, e) => o.OnValueChanged(e));
} }
private void OnValueChanged(AvaloniaPropertyChangedEventArgs<object?> args)
{
if (_updateFromComboBox) return;
var newValue = args.NewValue.Value;
if (newValue is null)
{
SetCurrentValue(SelectedValueProperty, null);
}
else
{
if (newValue.GetType() != EnumType)
{
SetCurrentValue(SelectedValueProperty, null);
}
var tuple = Values?.FirstOrDefault(x => Equals(x.Value, newValue));
SetCurrentValue(SelectedValueProperty, tuple);
}
}
private bool _updateFromComboBox;
private void OnSelectedValueChanged(AvaloniaPropertyChangedEventArgs<EnumItemTuple?> args)
{
_updateFromComboBox = true;
var newValue = args.NewValue.Value;
SetCurrentValue(ValueProperty, newValue?.Value);
_updateFromComboBox = false;
}
private void OnTypeChanged(AvaloniaPropertyChangedEventArgs<Type?> args) private void OnTypeChanged(AvaloniaPropertyChangedEventArgs<Type?> args)
{ {
Values?.Clear();
var newType = args.GetNewValue<Type?>(); var newType = args.GetNewValue<Type?>();
if (newType is null || !newType.IsEnum) if (newType is null || !newType.IsEnum)
{ {
return; return;
} }
Values = GenerateItemTuple();
}
var values = Enum.GetValues(newType); private List<EnumItemTuple> GenerateItemTuple()
List<object?> list = new(); {
if (EnumType is null) return new List<EnumItemTuple>();
var values = Enum.GetValues(EnumType);
List<EnumItemTuple> list = new();
var fields = EnumType.GetFields();
foreach (var value in values) foreach (var value in values)
{ {
if (value.GetType() == newType) if (value.GetType() == EnumType)
list.Add(value); {
var displayName = value.ToString();
var field = EnumType.GetField(displayName);
var description = field?.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault();
if (description is not null)
{
displayName = ((DescriptionAttribute) description).Description;
} }
Values = list; list.Add(new EnumItemTuple()
{
DisplayName = displayName,
Value = value
});
}
}
return list;
} }
} }