From 456390b775c76afb60c86dfa8ae19d791c3a8a7f Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Sat, 7 Jan 2023 23:43:22 +0800 Subject: [PATCH] enhance: add ListBoxAttach. --- .../Controls/Attach/ListBoxAttach.cs | 66 +++++++++++++++++++ .../HandyControl_Shared.projitems | 1 + 2 files changed, 67 insertions(+) create mode 100644 src/Shared/HandyControl_Shared/Controls/Attach/ListBoxAttach.cs diff --git a/src/Shared/HandyControl_Shared/Controls/Attach/ListBoxAttach.cs b/src/Shared/HandyControl_Shared/Controls/Attach/ListBoxAttach.cs new file mode 100644 index 00000000..a7496950 --- /dev/null +++ b/src/Shared/HandyControl_Shared/Controls/Attach/ListBoxAttach.cs @@ -0,0 +1,66 @@ +using System.Collections; +using System.Linq; +using System.Windows; +using System.Windows.Controls; +using HandyControl.Data; + +namespace HandyControl.Controls; + +public class ListBoxAttach +{ + public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.RegisterAttached( + "SelectedItems", typeof(IList), typeof(ListBoxAttach), + new FrameworkPropertyMetadata(default(IList), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, + OnSelectedItemsChanged)); + + public static void SetSelectedItems(DependencyObject element, IList value) + => element.SetValue(SelectedItemsProperty, value); + + public static IList GetSelectedItems(DependencyObject element) + => (IList) element.GetValue(SelectedItemsProperty); + + internal static readonly DependencyProperty InternalActionProperty = DependencyProperty.RegisterAttached( + "InternalAction", typeof(bool), typeof(ListBoxAttach), new PropertyMetadata(ValueBoxes.FalseBox)); + + internal static void SetInternalAction(DependencyObject element, bool value) + => element.SetValue(InternalActionProperty, ValueBoxes.BooleanBox(value)); + + internal static bool GetInternalAction(DependencyObject element) + => (bool) element.GetValue(InternalActionProperty); + + private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + if (d is not ListBox listBox) + { + return; + } + + if (GetInternalAction(listBox)) + { + return; + } + + listBox.SelectionChanged -= OnListBoxSelectionChanged; + listBox.SelectedItems.Clear(); + + if (e.NewValue is IList selectedItems) + { + foreach (object selectedItem in selectedItems) + { + listBox.SelectedItems.Add(selectedItem); + } + } + + listBox.SelectionChanged += OnListBoxSelectionChanged; + } + + private static void OnListBoxSelectionChanged(object sender, SelectionChangedEventArgs e) + { + if (sender is ListBox listBox) + { + SetInternalAction(listBox, true); + SetSelectedItems(listBox, listBox.SelectedItems.Cast().ToArray()); + SetInternalAction(listBox, false); + } + } +} diff --git a/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems b/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems index 7a134535..d6a57cce 100644 --- a/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems +++ b/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems @@ -21,6 +21,7 @@ +