From 29e96ba74382a35d80c7945fcdb657faf8306e16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=97=AB=E9=A9=9A=E9=8F=B5?= Date: Sun, 1 Dec 2024 21:33:26 +0800 Subject: [PATCH] =?UTF-8?q?Fix=20issue=20=EF=BC=88#124=20=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix issue (#124 ) --- .../MultiSelectSearchComboBox.cs | 48 +++++++++++-------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/src/WPFDevelopers.Shared/Controls/MultiSelectionSearchComboBox/MultiSelectSearchComboBox.cs b/src/WPFDevelopers.Shared/Controls/MultiSelectionSearchComboBox/MultiSelectSearchComboBox.cs index 6fd5046e..f0cd05f6 100644 --- a/src/WPFDevelopers.Shared/Controls/MultiSelectionSearchComboBox/MultiSelectSearchComboBox.cs +++ b/src/WPFDevelopers.Shared/Controls/MultiSelectionSearchComboBox/MultiSelectSearchComboBox.cs @@ -79,7 +79,7 @@ public class MultiSelectionSearchComboBox : Control public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register("SelectedItems", typeof(IList), typeof(MultiSelectionSearchComboBox), - new FrameworkPropertyMetadata(null, + new FrameworkPropertyMetadata(new ArrayList(), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal, OnSelectedItemsChanged)); @@ -109,37 +109,37 @@ static MultiSelectionSearchComboBox() public string Delimiter { - get => (string) GetValue(DelimiterProperty); + get => (string)GetValue(DelimiterProperty); set => SetValue(DelimiterProperty, value); } public string SelectedValuePath { - get => (string) GetValue(SelectedValuePathProperty); + get => (string)GetValue(SelectedValuePathProperty); set => SetValue(SelectedValuePathProperty, value); } public string DisplayMemberPath { - get => (string) GetValue(DisplayMemberPathProperty); + get => (string)GetValue(DisplayMemberPathProperty); set => SetValue(DisplayMemberPathProperty, value); } public string Text { - get => (string) GetValue(TextProperty); + get => (string)GetValue(TextProperty); set => SetValue(TextProperty, value); } public IEnumerable ItemsSource { - get => (IEnumerable) GetValue(ItemsSourceProperty); + get => (IEnumerable)GetValue(ItemsSourceProperty); set => SetValue(ItemsSourceProperty, value); } public IEnumerable ItemsSourceSearch { - get => (IEnumerable) GetValue(ItemsSourceSearchProperty); + get => (IEnumerable)GetValue(ItemsSourceSearchProperty); set => SetValue(ItemsSourceSearchProperty, value); } @@ -151,31 +151,31 @@ public object SelectAllContent public bool IsSelectAllActive { - get => (bool) GetValue(IsSelectAllActiveProperty); + get => (bool)GetValue(IsSelectAllActiveProperty); set => SetValue(IsSelectAllActiveProperty, value); } public bool IsDropDownOpen { - get => (bool) GetValue(IsDropDownOpenProperty); + get => (bool)GetValue(IsDropDownOpenProperty); set => SetValue(IsDropDownOpenProperty, value); } public double MaxDropDownHeight { - get => (double) GetValue(MaxDropDownHeightProperty); + get => (double)GetValue(MaxDropDownHeightProperty); set => SetValue(MaxDropDownHeightProperty, value); } public IList SelectedItems { - get => (IList) GetValue(SelectedItemsProperty); + get => (IList)GetValue(SelectedItemsProperty); set => SetValue(SelectedItemsProperty, value); } public string SearchWatermark { - get => (string) GetValue(SearchWatermarkProperty); + get => (string)GetValue(SearchWatermarkProperty); set => SetValue(SearchWatermarkProperty, value); } @@ -236,18 +236,18 @@ public override void OnApplyTemplate() private void Instance_PropertyChanged(object sender, PropertyChangedEventArgs e) { - SelectAllContent = LanguageManager.Instance["SelectAll"]; + SelectAllContent = LanguageManager.Instance["SelectAll"]; } private void OnListBoxSearch_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) { - if ((bool) e.NewValue) + if ((bool)e.NewValue) UpdateIsChecked(_listBoxSearch); } private void OnListBox_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) { - if ((bool) e.NewValue) + if ((bool)e.NewValue) { foreach (var item in selectedSearchList) if (!_listBox.SelectedItems.Contains(item)) @@ -277,7 +277,7 @@ private void UpdateIsChecked(ListBox listBox) private void OnPopup_GotFocus(object sender, RoutedEventArgs e) { - var source = (HwndSource) PresentationSource.FromVisual(_popup.Child); + var source = (HwndSource)PresentationSource.FromVisual(_popup.Child); if (source != null) { SetFocus(source.Handle); @@ -356,7 +356,17 @@ private void OnListBox_SelectionChanged(object sender, SelectionChangedEventArgs if (e.AddedItems.Count > 0) SelectionChecked(_listBox); Combination(); - SelectedItems = _listBox.SelectedItems; + var selectedItems = _listBox.SelectedItems; + if (SelectedItems == null) + SelectedItems = selectedItems; + else + { + foreach (var item in selectedItems) + { + if (!SelectedItems.Contains(item)) + SelectedItems.Add(item); + } + } } private void OnListBoxSearch_SelectionChanged(object sender, SelectionChangedEventArgs e) @@ -497,7 +507,7 @@ private static void OnIsDropDownOpenChanged(DependencyObject o, DependencyProper { var multiSelectionSearchComboBox = o as MultiSelectionSearchComboBox; if (multiSelectionSearchComboBox != null) - multiSelectionSearchComboBox.OnIsOpenChanged((bool) e.OldValue, (bool) e.NewValue); + multiSelectionSearchComboBox.OnIsOpenChanged((bool)e.OldValue, (bool)e.NewValue); } protected virtual void OnIsOpenChanged(bool oldValue, bool newValue) @@ -516,7 +526,7 @@ private static void OnMaxDropDownHeightChanged(DependencyObject o, DependencyPro { var comboBox = o as MultiSelectionSearchComboBox; if (comboBox != null) - comboBox.OnMaxDropDownHeightChanged((double) e.OldValue, (double) e.NewValue); + comboBox.OnMaxDropDownHeightChanged((double)e.OldValue, (double)e.NewValue); } protected virtual void OnMaxDropDownHeightChanged(double oldValue, double newValue)