@@ -8,6 +8,7 @@ public static class MenuKeys
|
|||||||
public const string MenuKeyButtonGroup = "ButtonGroup";
|
public const string MenuKeyButtonGroup = "ButtonGroup";
|
||||||
public const string MenuKeyDivider = "Divider";
|
public const string MenuKeyDivider = "Divider";
|
||||||
public const string MenuKeyDualBadge = "DualBadge";
|
public const string MenuKeyDualBadge = "DualBadge";
|
||||||
|
public const string MenuKeyEnumSelector = "EnumSelector";
|
||||||
public const string MenuKeyImageViewer = "ImageViewer";
|
public const string MenuKeyImageViewer = "ImageViewer";
|
||||||
public const string MenuKeyIpBox = "IPv4Box";
|
public const string MenuKeyIpBox = "IPv4Box";
|
||||||
public const string MenuKeyIconButton = "IconButton";
|
public const string MenuKeyIconButton = "IconButton";
|
||||||
|
|||||||
30
demo/Ursa.Demo/Pages/EnumSelectorDemo.axaml
Normal file
30
demo/Ursa.Demo/Pages/EnumSelectorDemo.axaml
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
<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>
|
||||||
|
<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>
|
||||||
13
demo/Ursa.Demo/Pages/EnumSelectorDemo.axaml.cs
Normal file
13
demo/Ursa.Demo/Pages/EnumSelectorDemo.axaml.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
using Avalonia;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Markup.Xaml;
|
||||||
|
|
||||||
|
namespace Ursa.Demo.Pages;
|
||||||
|
|
||||||
|
public partial class EnumSelectorDemo : UserControl
|
||||||
|
{
|
||||||
|
public EnumSelectorDemo()
|
||||||
|
{
|
||||||
|
InitializeComponent();
|
||||||
|
}
|
||||||
|
}
|
||||||
67
demo/Ursa.Demo/ViewModels/EnumSelectorDemoViewModel.cs
Normal file
67
demo/Ursa.Demo/ViewModels/EnumSelectorDemoViewModel.cs
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.ObjectModel;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.Windows.Input;
|
||||||
|
using Avalonia.Animation;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Data;
|
||||||
|
using Avalonia.Input;
|
||||||
|
using Avalonia.Interactivity;
|
||||||
|
using Avalonia.Layout;
|
||||||
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
|
using CommunityToolkit.Mvvm.Input;
|
||||||
|
|
||||||
|
namespace Ursa.Demo.ViewModels;
|
||||||
|
|
||||||
|
public class EnumSelectorDemoViewModel: ObservableObject
|
||||||
|
{
|
||||||
|
public ObservableCollection<Type?> Types { get; set; }
|
||||||
|
|
||||||
|
private Type? _selectedType;
|
||||||
|
public Type? SelectedType
|
||||||
|
{
|
||||||
|
get => _selectedType;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
SetProperty(ref _selectedType, value);
|
||||||
|
Value = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private object? _value;
|
||||||
|
public object? Value
|
||||||
|
{
|
||||||
|
get => _value;
|
||||||
|
set => SetProperty(ref _value, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
{
|
||||||
|
[Description("是")]
|
||||||
|
Yes,
|
||||||
|
[Description("否")]
|
||||||
|
No,
|
||||||
|
}
|
||||||
@@ -30,6 +30,7 @@ public class MainViewViewModel : ViewModelBase
|
|||||||
MenuKeys.MenuKeyButtonGroup => new ButtonGroupDemoViewModel(),
|
MenuKeys.MenuKeyButtonGroup => new ButtonGroupDemoViewModel(),
|
||||||
MenuKeys.MenuKeyDivider => new DividerDemoViewModel(),
|
MenuKeys.MenuKeyDivider => new DividerDemoViewModel(),
|
||||||
MenuKeys.MenuKeyDualBadge => new DualBadgeDemoViewModel(),
|
MenuKeys.MenuKeyDualBadge => new DualBadgeDemoViewModel(),
|
||||||
|
MenuKeys.MenuKeyEnumSelector => new EnumSelectorDemoViewModel(),
|
||||||
MenuKeys.MenuKeyImageViewer => new ImageViewerDemoViewModel(),
|
MenuKeys.MenuKeyImageViewer => new ImageViewerDemoViewModel(),
|
||||||
MenuKeys.MenuKeyIconButton => new IconButtonDemoViewModel(),
|
MenuKeys.MenuKeyIconButton => new IconButtonDemoViewModel(),
|
||||||
MenuKeys.MenuKeyIpBox => new IPv4BoxDemoViewModel(),
|
MenuKeys.MenuKeyIpBox => new IPv4BoxDemoViewModel(),
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ public class MenuViewModel: ViewModelBase
|
|||||||
new() { MenuHeader = "ButtonGroup", Key = MenuKeys.MenuKeyButtonGroup },
|
new() { MenuHeader = "ButtonGroup", Key = MenuKeys.MenuKeyButtonGroup },
|
||||||
new() { MenuHeader = "Divider", Key = MenuKeys.MenuKeyDivider },
|
new() { MenuHeader = "Divider", Key = MenuKeys.MenuKeyDivider },
|
||||||
new() { MenuHeader = "DualBadge", Key = MenuKeys.MenuKeyDualBadge },
|
new() { MenuHeader = "DualBadge", Key = MenuKeys.MenuKeyDualBadge },
|
||||||
|
new() { MenuHeader = "Enum Selector", Key = MenuKeys.MenuKeyEnumSelector },
|
||||||
new() { MenuHeader = "IconButton", Key = MenuKeys.MenuKeyIconButton },
|
new() { MenuHeader = "IconButton", Key = MenuKeys.MenuKeyIconButton },
|
||||||
new() { MenuHeader = "ImageViewer", Key = MenuKeys.MenuKeyImageViewer },
|
new() { MenuHeader = "ImageViewer", Key = MenuKeys.MenuKeyImageViewer },
|
||||||
new() { MenuHeader = "IPv4Box", Key = MenuKeys.MenuKeyIpBox },
|
new() { MenuHeader = "IPv4Box", Key = MenuKeys.MenuKeyIpBox },
|
||||||
|
|||||||
33
src/Ursa.Themes.Semi/Controls/EnumSelector.axaml
Normal file
33
src/Ursa.Themes.Semi/Controls/EnumSelector.axaml
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
<ResourceDictionary
|
||||||
|
xmlns="https://github.com/avaloniaui"
|
||||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
|
xmlns:u="https://irihi.tech/ursa">
|
||||||
|
<!-- Add Resources Here -->
|
||||||
|
<ControlTheme x:Key="{x:Type u:EnumSelector}" TargetType="u:EnumSelector">
|
||||||
|
<Setter Property="Width" Value="100" />
|
||||||
|
<Setter Property="HorizontalAlignment" Value="Left"></Setter>
|
||||||
|
<Setter Property="Template">
|
||||||
|
<ControlTemplate TargetType="u:EnumSelector">
|
||||||
|
<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>
|
||||||
@@ -6,6 +6,7 @@
|
|||||||
<ResourceInclude Source="ButtonGroup.axaml" />
|
<ResourceInclude Source="ButtonGroup.axaml" />
|
||||||
<ResourceInclude Source="Divider.axaml" />
|
<ResourceInclude Source="Divider.axaml" />
|
||||||
<ResourceInclude Source="DualBadge.axaml" />
|
<ResourceInclude Source="DualBadge.axaml" />
|
||||||
|
<ResourceInclude Source="EnumSelector.axaml" />
|
||||||
<ResourceInclude Source="IconButton.axaml" />
|
<ResourceInclude Source="IconButton.axaml" />
|
||||||
<ResourceInclude Source="ImageViewer.axaml" />
|
<ResourceInclude Source="ImageViewer.axaml" />
|
||||||
<ResourceInclude Source="IPv4Box.axaml" />
|
<ResourceInclude Source="IPv4Box.axaml" />
|
||||||
|
|||||||
158
src/Ursa/Controls/EnumSelector/EnumSelector.cs
Normal file
158
src/Ursa/Controls/EnumSelector/EnumSelector.cs
Normal file
@@ -0,0 +1,158 @@
|
|||||||
|
using System.ComponentModel;
|
||||||
|
using Avalonia;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Controls.Primitives;
|
||||||
|
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?>(
|
||||||
|
nameof(EnumType), validate: OnTypeValidate);
|
||||||
|
|
||||||
|
public Type? EnumType
|
||||||
|
{
|
||||||
|
get => GetValue(EnumTypeProperty);
|
||||||
|
set => SetValue(EnumTypeProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool OnTypeValidate(Type? arg)
|
||||||
|
{
|
||||||
|
if (arg is null) return true;
|
||||||
|
return arg.IsEnum;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static readonly StyledProperty<object?> ValueProperty = AvaloniaProperty.Register<EnumSelector, object?>(
|
||||||
|
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<EnumItemTuple>?> ValuesProperty = AvaloniaProperty.RegisterDirect<EnumSelector, IList<EnumItemTuple>?>(
|
||||||
|
nameof(Values), o => o.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();
|
||||||
|
}
|
||||||
|
|
||||||
|
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() == 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
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user