feat: add value validation.
This commit is contained in:
@@ -1,19 +1,30 @@
|
||||
<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"
|
||||
mc:Ignorable="d" d:DesignWidth="800"
|
||||
xmlns:vm="clr-namespace:Ursa.Demo.ViewModels;assembly=Ursa.Demo"
|
||||
xmlns:u="https://irihi.tech/ursa"
|
||||
x:DataType="vm:EnumSelectorDemoViewModel"
|
||||
x:CompileBindings="True"
|
||||
d:DesignHeight="450"
|
||||
x:Class="Ursa.Demo.Pages.EnumSelectorDemo">
|
||||
<UserControl
|
||||
x:Class="Ursa.Demo.Pages.EnumSelectorDemo"
|
||||
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:u="https://irihi.tech/ursa"
|
||||
xmlns:vm="clr-namespace:Ursa.Demo.ViewModels;assembly=Ursa.Demo"
|
||||
d:DesignHeight="450"
|
||||
d:DesignWidth="800"
|
||||
x:CompileBindings="True"
|
||||
x:DataType="vm:EnumSelectorDemoViewModel"
|
||||
mc:Ignorable="d">
|
||||
<StackPanel>
|
||||
<TextBlock Text="Select Type"></TextBlock>
|
||||
<ComboBox ItemsSource="{Binding Types}" DisplayMemberBinding="{Binding Name}" SelectedItem="{Binding SelectedType}"/>
|
||||
<TextBlock Text="Select Value"></TextBlock>
|
||||
<u:EnumSelector EnumType="{Binding SelectedType}" Value="{Binding Value}" />
|
||||
<TextBlock Text="{Binding Value}"></TextBlock>
|
||||
<ToggleSwitch Name="description" Content="Show Description" />
|
||||
<TextBlock Text="Select Type" />
|
||||
<ComboBox
|
||||
Width="200"
|
||||
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>
|
||||
</UserControl>
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Windows.Input;
|
||||
using Avalonia.Animation;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Data;
|
||||
@@ -7,6 +9,7 @@ using Avalonia.Input;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Layout;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
|
||||
namespace Ursa.Demo.ViewModels;
|
||||
|
||||
@@ -50,6 +53,15 @@ public class EnumSelectorDemoViewModel: ObservableObject
|
||||
typeof(Key),
|
||||
typeof(KeyModifiers),
|
||||
typeof(RoutingStrategies),
|
||||
typeof(CustomEnum),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public enum CustomEnum
|
||||
{
|
||||
[Description("是")]
|
||||
Yes,
|
||||
[Description("否")]
|
||||
No,
|
||||
}
|
||||
@@ -4,10 +4,29 @@
|
||||
xmlns:u="https://irihi.tech/ursa">
|
||||
<!-- Add Resources Here -->
|
||||
<ControlTheme x:Key="{x:Type u:EnumSelector}" TargetType="u:EnumSelector">
|
||||
<Setter Property="HorizontalAlignment" Value="Left"></Setter>
|
||||
<Setter Property="Template">
|
||||
<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>
|
||||
</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>
|
||||
</ResourceDictionary>
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.ComponentModel;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
@@ -5,6 +6,12 @@ using Avalonia.Data;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
public class EnumItemTuple
|
||||
{
|
||||
public string? DisplayName { get; set; }
|
||||
public object? Value { get; set; }
|
||||
}
|
||||
|
||||
public class EnumSelector: TemplatedControl
|
||||
{
|
||||
public static readonly StyledProperty<Type?> EnumTypeProperty = AvaloniaProperty.Register<EnumSelector, Type?>(
|
||||
@@ -23,45 +30,129 @@ public class EnumSelector: TemplatedControl
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
get => GetValue(ValueProperty);
|
||||
set => SetValue(ValueProperty, value);
|
||||
}
|
||||
|
||||
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<object?>?> ValuesProperty = AvaloniaProperty.RegisterDirect<EnumSelector, IList<object?>?>(
|
||||
public static readonly DirectProperty<EnumSelector, IList<EnumItemTuple>?> ValuesProperty = AvaloniaProperty.RegisterDirect<EnumSelector, IList<EnumItemTuple>?>(
|
||||
nameof(Values), o => o.Values);
|
||||
|
||||
private IList<object?>? _values;
|
||||
internal IList<object?>? Values
|
||||
private IList<EnumItemTuple>? _values;
|
||||
internal IList<EnumItemTuple>? Values
|
||||
{
|
||||
get => _values;
|
||||
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()
|
||||
{
|
||||
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)
|
||||
{
|
||||
Values?.Clear();
|
||||
var newType = args.GetNewValue<Type?>();
|
||||
if (newType is null || !newType.IsEnum)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Values = GenerateItemTuple();
|
||||
}
|
||||
|
||||
var values = Enum.GetValues(newType);
|
||||
List<object?> list = new();
|
||||
private List<EnumItemTuple> GenerateItemTuple()
|
||||
{
|
||||
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)
|
||||
{
|
||||
if (value.GetType() == newType)
|
||||
list.Add(value);
|
||||
if (value.GetType() == EnumType)
|
||||
{
|
||||
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;
|
||||
}
|
||||
list.Add(new EnumItemTuple()
|
||||
{
|
||||
DisplayName = displayName,
|
||||
Value = value
|
||||
});
|
||||
}
|
||||
}
|
||||
Values = list;
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user