Merge pull request #671 from irihitech/enum
Ensure initial state is respected in EnumSelector
This commit is contained in:
@@ -26,7 +26,7 @@
|
|||||||
EnumType="{Binding SelectedType}"
|
EnumType="{Binding SelectedType}"
|
||||||
Value="{Binding Value}" />
|
Value="{Binding Value}" />
|
||||||
<TextBlock Text="{Binding Value}" />
|
<TextBlock Text="{Binding Value}" />
|
||||||
<u:Divider Content="Small Size"></u:Divider>
|
<u:Divider Content="Small Size" />
|
||||||
<u:EnumSelector
|
<u:EnumSelector
|
||||||
Width="200"
|
Width="200"
|
||||||
Classes="Small"
|
Classes="Small"
|
||||||
|
|||||||
@@ -11,55 +11,33 @@ using CommunityToolkit.Mvvm.ComponentModel;
|
|||||||
|
|
||||||
namespace Ursa.Demo.ViewModels;
|
namespace Ursa.Demo.ViewModels;
|
||||||
|
|
||||||
public class EnumSelectorDemoViewModel: ObservableObject
|
public partial class EnumSelectorDemoViewModel : ObservableObject
|
||||||
{
|
{
|
||||||
public ObservableCollection<Type?> Types { get; set; }
|
[ObservableProperty] private Type? _selectedType;
|
||||||
|
[ObservableProperty] private object? _value;
|
||||||
|
|
||||||
private Type? _selectedType;
|
public ObservableCollection<Type?> Types { get; set; } =
|
||||||
public Type? SelectedType
|
[
|
||||||
{
|
typeof(HorizontalAlignment),
|
||||||
get => _selectedType;
|
typeof(VerticalAlignment),
|
||||||
set
|
typeof(Orientation),
|
||||||
{
|
typeof(Dock),
|
||||||
SetProperty(ref _selectedType, value);
|
typeof(GridResizeDirection),
|
||||||
Value = null;
|
typeof(DayOfWeek),
|
||||||
}
|
typeof(FillMode),
|
||||||
}
|
typeof(IterationType),
|
||||||
|
typeof(BindingMode),
|
||||||
private object? _value;
|
typeof(BindingPriority),
|
||||||
public object? Value
|
typeof(StandardCursorType),
|
||||||
{
|
typeof(Key),
|
||||||
get => _value;
|
typeof(KeyModifiers),
|
||||||
set => SetProperty(ref _value, value);
|
typeof(RoutingStrategies),
|
||||||
}
|
typeof(CustomEnum)
|
||||||
|
];
|
||||||
public EnumSelectorDemoViewModel()
|
|
||||||
{
|
|
||||||
Types = new ObservableCollection<Type?>()
|
|
||||||
{
|
|
||||||
typeof(HorizontalAlignment),
|
|
||||||
typeof(VerticalAlignment),
|
|
||||||
typeof(Orientation),
|
|
||||||
typeof(Dock),
|
|
||||||
typeof(GridResizeDirection),
|
|
||||||
typeof(DayOfWeek),
|
|
||||||
typeof(FillMode),
|
|
||||||
typeof(IterationType),
|
|
||||||
typeof(BindingMode),
|
|
||||||
typeof(BindingPriority),
|
|
||||||
typeof(StandardCursorType),
|
|
||||||
typeof(Key),
|
|
||||||
typeof(KeyModifiers),
|
|
||||||
typeof(RoutingStrategies),
|
|
||||||
typeof(CustomEnum),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum CustomEnum
|
public enum CustomEnum
|
||||||
{
|
{
|
||||||
[Description("是")]
|
[Description("是")] Yes,
|
||||||
Yes,
|
[Description("否")] No,
|
||||||
[Description("否")]
|
|
||||||
No,
|
|
||||||
}
|
}
|
||||||
@@ -34,6 +34,7 @@ public class EnumSelector: TemplatedControl
|
|||||||
private static object? OnValueCoerce(AvaloniaObject o, object? value)
|
private static object? OnValueCoerce(AvaloniaObject o, object? value)
|
||||||
{
|
{
|
||||||
if (o is not EnumSelector selector) return null;
|
if (o is not EnumSelector selector) return null;
|
||||||
|
if (!selector.IsInitialized) return value;
|
||||||
if (value is null) return null;
|
if (value is null) return null;
|
||||||
if (value.GetType() != selector.EnumType) return null;
|
if (value.GetType() != selector.EnumType) return null;
|
||||||
var first = selector.Values?.FirstOrDefault(a => Equals(a.Value, value));
|
var first = selector.Values?.FirstOrDefault(a => Equals(a.Value, value));
|
||||||
@@ -128,6 +129,8 @@ public class EnumSelector: TemplatedControl
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Values = GenerateItemTuple();
|
Values = GenerateItemTuple();
|
||||||
|
var first = Values?.FirstOrDefault(a => Equals(a.Value, this.Value));
|
||||||
|
SetCurrentValue(SelectedValueProperty, first);
|
||||||
}
|
}
|
||||||
|
|
||||||
// netstandard 2.0 does not support Enum.GetValuesAsUnderlyingType, which is used for native aot compilation
|
// netstandard 2.0 does not support Enum.GetValuesAsUnderlyingType, which is used for native aot compilation
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Headless.XUnit;
|
||||||
|
using Ursa.Controls;
|
||||||
|
|
||||||
|
namespace HeadlessTest.Ursa.Controls.EnumSelectorTests;
|
||||||
|
|
||||||
|
public class InitialStateTest
|
||||||
|
{
|
||||||
|
[AvaloniaFact]
|
||||||
|
public void Initial_State_Respected()
|
||||||
|
{
|
||||||
|
var window = new Window();
|
||||||
|
var selector = new EnumSelector()
|
||||||
|
{
|
||||||
|
Value = HorizontalPosition.Right,
|
||||||
|
EnumType = typeof(HorizontalPosition),
|
||||||
|
};
|
||||||
|
window.Content = selector;
|
||||||
|
window.Show();
|
||||||
|
Assert.Equal(HorizontalPosition.Right, selector.Value);
|
||||||
|
Assert.NotNull(selector.SelectedValue);
|
||||||
|
Assert.Equal(HorizontalPosition.Right, selector.SelectedValue?.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user