Browse Source

Added support for deleting recent tags in TagsEditor

Kenric Nugteren 5 months ago
parent
commit
0f0d2e7063
1 changed files with 41 additions and 0 deletions
  1. 41 0
      inabox.wpf/DynamicGrid/Editors/TagsEditor/TagsEditorControl.cs

+ 41 - 0
inabox.wpf/DynamicGrid/Editors/TagsEditor/TagsEditorControl.cs

@@ -1,4 +1,5 @@
 using InABox.Core;
+using InABox.Wpf;
 using InABox.WPF;
 using Syncfusion.Data.Extensions;
 using System;
@@ -72,6 +73,35 @@ public class TagsEditorControl : DynamicEditorControl<string, TagsEditor>
             TagList = new ListBox();
             TagList.Width = Editor.ActualWidth;
             TagList.Height = 100;
+            TagList.HorizontalContentAlignment = HorizontalAlignment.Stretch;
+            TagList.ItemTemplate = TemplateGenerator.CreateDataTemplate(() =>
+            {
+                var panel = new DockPanel();
+
+                var item = new TextBlock();
+                item.Bind<string, string>(TextBlock.TextProperty, x => x);
+                DockPanel.SetDock(item, Dock.Left);
+
+                var btn = new Button();
+                btn.Content = "X";
+                btn.Width = 15;
+                btn.Height = 15;
+                btn.Padding = new();
+                btn.FontSize = 10;
+                btn.VerticalAlignment = VerticalAlignment.Center;
+                btn.VerticalContentAlignment = VerticalAlignment.Center;
+                btn.Background = Colors.Transparent.ToBrush();
+                btn.BorderBrush = Colors.Transparent.ToBrush();
+                btn.Bind<string, string>(Button.TagProperty, x => x);
+                btn.Bind(Button.VisibilityProperty, panel, x => x.IsMouseOver, new WPF.BooleanToVisibilityConverter(Visibility.Visible, Visibility.Collapsed));
+                DockPanel.SetDock(btn, Dock.Right);
+                btn.Click += DeleteTag_Click;
+
+                panel.Children.Add(btn);
+                panel.Children.Add(item);
+
+                return panel;
+            });
             TagList.ItemsSource = RecentTags;
             TagList.MouseUp += TagList_MouseUp;
 
@@ -82,6 +112,17 @@ public class TagsEditorControl : DynamicEditorControl<string, TagsEditor>
         }
     }
 
+    private void DeleteTag_Click(object sender, RoutedEventArgs e)
+    {
+        if (sender is not Button btn || btn.Tag is not string tag) return;
+
+        if (RecentTags.Remove(tag))
+        {
+            OnRecentTagsChanged?.Invoke(RecentTags);
+            UpdateTagList();
+        }
+    }
+
     private void TagList_MouseUp(object sender, MouseButtonEventArgs e)
     {
         AddSelectedTag();