|
@@ -29,6 +29,10 @@ public partial class ButtonStrip : TemplatedControl
|
|
|
nameof(ItemSpacing));
|
|
|
public static StyledProperty<ObservableCollection<ButtonStripItem>> ItemsProperty =
|
|
|
AvaloniaProperty.Register<ButtonStripItem, ObservableCollection<ButtonStripItem>>(nameof(Items));
|
|
|
+ public static StyledProperty<IEnumerable?> ItemsSourceProperty =
|
|
|
+ AvaloniaProperty.Register<ButtonStripItem, IEnumerable?>(nameof(ItemsSource));
|
|
|
+ public static StyledProperty<ICommand> SelectedCommandProperty = AvaloniaProperty.Register<ButtonStrip, ICommand>(nameof(SelectedCommand));
|
|
|
+ public static StyledProperty<object?> SelectedItemProperty = AvaloniaProperty.Register<ButtonStrip, object?>(nameof(SelectedItem));
|
|
|
|
|
|
public IBrush? SelectedBackground
|
|
|
{
|
|
@@ -45,6 +49,17 @@ public partial class ButtonStrip : TemplatedControl
|
|
|
get => GetValue(ItemSpacingProperty);
|
|
|
set => SetValue(ItemSpacingProperty, value);
|
|
|
}
|
|
|
+ public ICommand SelectedCommand
|
|
|
+ {
|
|
|
+ get => GetValue(SelectedCommandProperty);
|
|
|
+ set => SetValue(SelectedCommandProperty, value);
|
|
|
+ }
|
|
|
+
|
|
|
+ public IEnumerable? ItemsSource
|
|
|
+ {
|
|
|
+ get => GetValue(ItemsSourceProperty);
|
|
|
+ set => SetValue(ItemsSourceProperty, value);
|
|
|
+ }
|
|
|
|
|
|
[Content]
|
|
|
public ObservableCollection<ButtonStripItem> Items
|
|
@@ -53,7 +68,7 @@ public partial class ButtonStrip : TemplatedControl
|
|
|
set => SetValue(ItemsProperty, value);
|
|
|
}
|
|
|
|
|
|
- public ButtonStripItem? SelectedItem
|
|
|
+ private ButtonStripItem? SelectedButton
|
|
|
{
|
|
|
get => Items.FirstOrDefault(x => x.Selected);
|
|
|
set
|
|
@@ -62,14 +77,50 @@ public partial class ButtonStrip : TemplatedControl
|
|
|
{
|
|
|
item.Selected = item == value;
|
|
|
}
|
|
|
+ SelectedItem = value?.Tag;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public object? SelectedItem
|
|
|
+ {
|
|
|
+ get => GetValue(SelectedItemProperty);
|
|
|
+ set => SetValue(SelectedItemProperty, value);
|
|
|
+ }
|
|
|
+
|
|
|
public event EventHandler? SelectionChanged;
|
|
|
|
|
|
static ButtonStrip()
|
|
|
{
|
|
|
ItemsProperty.Changed.AddClassHandler<ButtonStrip>(Items_Changed);
|
|
|
+ ItemsSourceProperty.Changed.AddClassHandler<ButtonStrip>(ItemsSource_Changed);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void ItemsSource_Changed(ButtonStrip strip, AvaloniaPropertyChangedEventArgs args)
|
|
|
+ {
|
|
|
+ if(strip.ItemsSource is INotifyCollectionChanged notify)
|
|
|
+ {
|
|
|
+ notify.CollectionChanged += (sender, e) => strip.RebuildItems();
|
|
|
+ }
|
|
|
+ strip.RebuildItems();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void RebuildItems()
|
|
|
+ {
|
|
|
+ Items.Clear();
|
|
|
+ if(ItemsSource is not null)
|
|
|
+ {
|
|
|
+ foreach(var item in ItemsSource)
|
|
|
+ {
|
|
|
+ if(item is ButtonStripItem btn)
|
|
|
+ {
|
|
|
+ Items.Add(btn);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Items.Add(new(item?.ToString() ?? "") { Tag = item });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private static void Items_Changed(ButtonStrip strip, AvaloniaPropertyChangedEventArgs args)
|
|
@@ -77,7 +128,7 @@ public partial class ButtonStrip : TemplatedControl
|
|
|
strip.LogicalChildren.Clear();
|
|
|
if (strip.Items is not null)
|
|
|
{
|
|
|
- strip.SelectedItem = strip.Items.FirstOrDefault();
|
|
|
+ strip.SelectedButton = strip.Items.FirstOrDefault();
|
|
|
|
|
|
strip.LogicalChildren.AddRange(strip.Items);
|
|
|
foreach(var item in strip.Items)
|
|
@@ -95,7 +146,7 @@ public partial class ButtonStrip : TemplatedControl
|
|
|
|
|
|
private void Items_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
|
|
|
{
|
|
|
- SelectedItem ??= Items.FirstOrDefault();
|
|
|
+ SelectedButton ??= Items.FirstOrDefault();
|
|
|
switch (e.Action)
|
|
|
{
|
|
|
case NotifyCollectionChangedAction.Add:
|
|
@@ -152,14 +203,16 @@ public partial class ButtonStrip : TemplatedControl
|
|
|
private void ItemSelected(ButtonStripItem item)
|
|
|
{
|
|
|
var children = this.GetLogicalChildren().ToArray();
|
|
|
- SelectedItem = item;
|
|
|
+ SelectedButton = item;
|
|
|
+ SelectionChanged?.Invoke(this, new());
|
|
|
+ SelectedCommand?.Execute(item.Tag);
|
|
|
}
|
|
|
|
|
|
public void AddRange(IEnumerable<string> items)
|
|
|
{
|
|
|
foreach(var item in items)
|
|
|
{
|
|
|
- Items.Add(new() { Text = item });
|
|
|
+ Items.Add(new(item));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -193,6 +246,15 @@ public class ButtonStripItem : TemplatedControl
|
|
|
set => SetValue(CommandProperty, value);
|
|
|
}
|
|
|
|
|
|
+ public ButtonStripItem()
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ public ButtonStripItem(string text)
|
|
|
+ {
|
|
|
+ Text = text;
|
|
|
+ }
|
|
|
+
|
|
|
static ButtonStripItem()
|
|
|
{
|
|
|
SelectedProperty.Changed.AddClassHandler<ButtonStripItem>(Selected_Changed);
|