feat: initialize and setup demo.

This commit is contained in:
rabbitism
2024-01-25 18:54:24 +08:00
parent f4baf18c87
commit d1bb258b28
9 changed files with 171 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
<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="Template">
<ControlTemplate TargetType="u:EnumSelector">
<ComboBox ItemsSource="{TemplateBinding Values}" SelectedItem="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value, Mode=TwoWay}" />
</ControlTemplate>
</Setter>
</ControlTheme>
</ResourceDictionary>

View File

@@ -6,6 +6,7 @@
<ResourceInclude Source="ButtonGroup.axaml" />
<ResourceInclude Source="Divider.axaml" />
<ResourceInclude Source="DualBadge.axaml" />
<ResourceInclude Source="EnumSelector.axaml" />
<ResourceInclude Source="IconButton.axaml" />
<ResourceInclude Source="ImageViewer.axaml" />
<ResourceInclude Source="IPv4Box.axaml" />

View File

@@ -0,0 +1,67 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Data;
namespace Ursa.Controls;
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);
public object? Value
{
get => GetValue(ValueProperty);
set => SetValue(ValueProperty, value);
}
public static readonly DirectProperty<EnumSelector, IList<object?>?> ValuesProperty = AvaloniaProperty.RegisterDirect<EnumSelector, IList<object?>?>(
nameof(Values), o => o.Values);
private IList<object?>? _values;
internal IList<object?>? Values
{
get => _values;
private set => SetAndRaise(ValuesProperty, ref _values, value);
}
static EnumSelector()
{
EnumTypeProperty.Changed.AddClassHandler<EnumSelector, Type?>((o, e) => o.OnTypeChanged(e));
}
private void OnTypeChanged(AvaloniaPropertyChangedEventArgs<Type?> args)
{
var newType = args.GetNewValue<Type?>();
if (newType is null || !newType.IsEnum)
{
return;
}
var values = Enum.GetValues(newType);
List<object?> list = new();
foreach (var value in values)
{
if (value.GetType() == newType)
list.Add(value);
}
Values = list;
}
}