diff --git a/src/MahApps.Metro/Controls/Helper/TextBoxHelper.cs b/src/MahApps.Metro/Controls/Helper/TextBoxHelper.cs index c5070df58e..544ee83822 100644 --- a/src/MahApps.Metro/Controls/Helper/TextBoxHelper.cs +++ b/src/MahApps.Metro/Controls/Helper/TextBoxHelper.cs @@ -1102,7 +1102,7 @@ public static void ButtonClicked(object sender, RoutedEventArgs e) { if (multiSelectionComboBox.HasCustomText) { - multiSelectionComboBox.ResetEditableText(); + multiSelectionComboBox.ResetEditableText(true); } else { @@ -1118,6 +1118,7 @@ public static void ButtonClicked(object sender, RoutedEventArgs e) default: throw new NotSupportedException("Unknown SelectionMode"); } + multiSelectionComboBox.ResetEditableText(true); } } else if (parent is ComboBox comboBox) diff --git a/src/MahApps.Metro/Controls/MultiSelectionComboBox/MultiSelectionComboBox.cs b/src/MahApps.Metro/Controls/MultiSelectionComboBox/MultiSelectionComboBox.cs index 87ff6dcdf5..e1dbf28e7b 100644 --- a/src/MahApps.Metro/Controls/MultiSelectionComboBox/MultiSelectionComboBox.cs +++ b/src/MahApps.Metro/Controls/MultiSelectionComboBox/MultiSelectionComboBox.cs @@ -532,7 +532,7 @@ public bool InterceptMouseWheelSelection /// /// Resets the custom Text to the selected Items text /// - public void ResetEditableText() + public void ResetEditableText(bool forceUpdate = false) { if (this.PART_EditableTextBox is not null) { @@ -540,7 +540,7 @@ public void ResetEditableText() var oldSelectionLength = this.PART_EditableTextBox.SelectionLength; this.SetValue(HasCustomTextPropertyKey, false); - this.UpdateEditableText(); + this.UpdateEditableText(forceUpdate); this.PART_EditableTextBox.SelectionStart = oldSelectionStart; this.PART_EditableTextBox.SelectionLength = oldSelectionLength; @@ -761,8 +761,6 @@ private void UpdateSelectedItemsFromTextTimer_Tick(object sender, EventArgs e) int position = 0; - // this.SelectedItems?.Clear(); - if (strings is not null) { foreach (var stringObject in strings) @@ -916,7 +914,7 @@ private static void ExecutedClearContentCommand(object sender, ExecutedRoutedEve { if (multiSelectionCombo.HasCustomText) { - multiSelectionCombo.ResetEditableText(); + multiSelectionCombo.ResetEditableText(true); } else { @@ -933,6 +931,7 @@ private static void ExecutedClearContentCommand(object sender, ExecutedRoutedEve throw new NotSupportedException("Unknown SelectionMode"); } } + multiSelectionCombo.ResetEditableText(true); } } @@ -1004,11 +1003,11 @@ public override void OnApplyTemplate() { selectedItemsCollection.CollectionChanged -= this.PART_PopupListBox_SelectedItems_CollectionChanged; selectedItemsCollection.CollectionChanged += this.PART_PopupListBox_SelectedItems_CollectionChanged; - - PART_PopupListBox.Unloaded += (s, e) => { selectedItemsCollection.CollectionChanged -= this.PART_PopupListBox_SelectedItems_CollectionChanged; }; } - // Do update the text + this.SyncSelectedItems(this.SelectedItems, PART_PopupListBox.SelectedItems, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); + + // Do update the text and selection this.UpdateDisplaySelectedItems(); this.UpdateEditableText(true); } @@ -1060,7 +1059,7 @@ protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e) } // If we have the ItemsSource set, we need to exit here. - if (this.PART_PopupListBox is null || ((this.PART_PopupListBox.Items as IList)?.IsReadOnly ?? false) || BindingOperations.IsDataBound(this.PART_PopupListBox, ItemsSourceProperty)) + if (((PART_PopupListBox?.Items as IList)?.IsReadOnly ?? false) || BindingOperations.IsDataBound(this, ItemsSourceProperty)) { return; } @@ -1072,7 +1071,7 @@ protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e) { foreach (var item in e.NewItems) { - this.PART_PopupListBox.Items.Add(item); + this.PART_PopupListBox?.Items?.Add(item); } } @@ -1083,7 +1082,7 @@ protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e) { foreach (var item in e.OldItems) { - this.PART_PopupListBox.Items.Remove(item); + this.PART_PopupListBox?.Items?.Remove(item); } } @@ -1092,10 +1091,10 @@ protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e) case NotifyCollectionChangedAction.Replace: case NotifyCollectionChangedAction.Move: case NotifyCollectionChangedAction.Reset: - this.PART_PopupListBox.Items.Clear(); + this.PART_PopupListBox?.Items?.Clear(); foreach (var item in this.Items) { - this.PART_PopupListBox.Items.Add(item); + this.PART_PopupListBox?.Items?.Add(item); } break; @@ -1579,15 +1578,15 @@ private void SelectedItemsImpl_CollectionChanged(object sender, NotifyCollection { if (this.PART_PopupListBox is null) { - this.ApplyTemplate(); + return; } - this.SyncSelectedItems(sender as IList, this.PART_PopupListBox?.SelectedItems, e); + this.SyncSelectedItems(sender as IList, this.PART_PopupListBox.SelectedItems, e); } private void SyncSelectedItems(IList? sourceCollection, IList? targetCollection, NotifyCollectionChangedEventArgs e) { - if (this.IsSyncingSelectedItems || sourceCollection is null || targetCollection is null) + if (this.IsSyncingSelectedItems || sourceCollection is null || targetCollection is null || !this.IsInitialized) { return; } diff --git a/src/MahApps.Metro/Themes/MultiSelectionComboBox.xaml b/src/MahApps.Metro/Themes/MultiSelectionComboBox.xaml index 4d60c7b06e..79941d4c39 100644 --- a/src/MahApps.Metro/Themes/MultiSelectionComboBox.xaml +++ b/src/MahApps.Metro/Themes/MultiSelectionComboBox.xaml @@ -22,7 +22,7 @@