Skip to content

Commit 48fab02

Browse files
committed
Improve ClearTextButton behaviour for MultiSelectionComboBox
1 parent 13a3ab8 commit 48fab02

3 files changed

Lines changed: 18 additions & 18 deletions

File tree

src/MahApps.Metro/Controls/Helper/TextBoxHelper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,7 @@ public static void ButtonClicked(object sender, RoutedEventArgs e)
11021102
{
11031103
if (multiSelectionComboBox.HasCustomText)
11041104
{
1105-
multiSelectionComboBox.ResetEditableText();
1105+
multiSelectionComboBox.ResetEditableText(true);
11061106
}
11071107
else
11081108
{
@@ -1118,6 +1118,7 @@ public static void ButtonClicked(object sender, RoutedEventArgs e)
11181118
default:
11191119
throw new NotSupportedException("Unknown SelectionMode");
11201120
}
1121+
multiSelectionComboBox.ResetEditableText(true);
11211122
}
11221123
}
11231124
else if (parent is ComboBox comboBox)

src/MahApps.Metro/Controls/MultiSelectionComboBox/MultiSelectionComboBox.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -532,15 +532,15 @@ public bool InterceptMouseWheelSelection
532532
/// <summary>
533533
/// Resets the custom Text to the selected Items text
534534
/// </summary>
535-
public void ResetEditableText()
535+
public void ResetEditableText(bool forceUpdate = false)
536536
{
537537
if (this.PART_EditableTextBox is not null)
538538
{
539539
var oldSelectionStart = this.PART_EditableTextBox.SelectionStart;
540540
var oldSelectionLength = this.PART_EditableTextBox.SelectionLength;
541541

542542
this.SetValue(HasCustomTextPropertyKey, false);
543-
this.UpdateEditableText();
543+
this.UpdateEditableText(forceUpdate);
544544

545545
this.PART_EditableTextBox.SelectionStart = oldSelectionStart;
546546
this.PART_EditableTextBox.SelectionLength = oldSelectionLength;
@@ -761,8 +761,6 @@ private void UpdateSelectedItemsFromTextTimer_Tick(object sender, EventArgs e)
761761

762762
int position = 0;
763763

764-
// this.SelectedItems?.Clear();
765-
766764
if (strings is not null)
767765
{
768766
foreach (var stringObject in strings)
@@ -916,7 +914,7 @@ private static void ExecutedClearContentCommand(object sender, ExecutedRoutedEve
916914
{
917915
if (multiSelectionCombo.HasCustomText)
918916
{
919-
multiSelectionCombo.ResetEditableText();
917+
multiSelectionCombo.ResetEditableText(true);
920918
}
921919
else
922920
{
@@ -933,6 +931,7 @@ private static void ExecutedClearContentCommand(object sender, ExecutedRoutedEve
933931
throw new NotSupportedException("Unknown SelectionMode");
934932
}
935933
}
934+
multiSelectionCombo.ResetEditableText(true);
936935
}
937936
}
938937

@@ -1004,11 +1003,11 @@ public override void OnApplyTemplate()
10041003
{
10051004
selectedItemsCollection.CollectionChanged -= this.PART_PopupListBox_SelectedItems_CollectionChanged;
10061005
selectedItemsCollection.CollectionChanged += this.PART_PopupListBox_SelectedItems_CollectionChanged;
1007-
1008-
PART_PopupListBox.Unloaded += (s, e) => { selectedItemsCollection.CollectionChanged -= this.PART_PopupListBox_SelectedItems_CollectionChanged; };
10091006
}
10101007

1011-
// Do update the text
1008+
this.SyncSelectedItems(this.SelectedItems, PART_PopupListBox.SelectedItems, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
1009+
1010+
// Do update the text and selection
10121011
this.UpdateDisplaySelectedItems();
10131012
this.UpdateEditableText(true);
10141013
}
@@ -1060,7 +1059,7 @@ protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
10601059
}
10611060

10621061
// If we have the ItemsSource set, we need to exit here.
1063-
if (this.PART_PopupListBox is null || ((this.PART_PopupListBox.Items as IList)?.IsReadOnly ?? false) || BindingOperations.IsDataBound(this.PART_PopupListBox, ItemsSourceProperty))
1062+
if (((PART_PopupListBox?.Items as IList)?.IsReadOnly ?? false) || BindingOperations.IsDataBound(this, ItemsSourceProperty))
10641063
{
10651064
return;
10661065
}
@@ -1072,7 +1071,7 @@ protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
10721071
{
10731072
foreach (var item in e.NewItems)
10741073
{
1075-
this.PART_PopupListBox.Items.Add(item);
1074+
this.PART_PopupListBox?.Items?.Add(item);
10761075
}
10771076
}
10781077

@@ -1083,7 +1082,7 @@ protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
10831082
{
10841083
foreach (var item in e.OldItems)
10851084
{
1086-
this.PART_PopupListBox.Items.Remove(item);
1085+
this.PART_PopupListBox?.Items?.Remove(item);
10871086
}
10881087
}
10891088

@@ -1092,10 +1091,10 @@ protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
10921091
case NotifyCollectionChangedAction.Replace:
10931092
case NotifyCollectionChangedAction.Move:
10941093
case NotifyCollectionChangedAction.Reset:
1095-
this.PART_PopupListBox.Items.Clear();
1094+
this.PART_PopupListBox?.Items?.Clear();
10961095
foreach (var item in this.Items)
10971096
{
1098-
this.PART_PopupListBox.Items.Add(item);
1097+
this.PART_PopupListBox?.Items?.Add(item);
10991098
}
11001099

11011100
break;
@@ -1579,15 +1578,15 @@ private void SelectedItemsImpl_CollectionChanged(object sender, NotifyCollection
15791578
{
15801579
if (this.PART_PopupListBox is null)
15811580
{
1582-
this.ApplyTemplate();
1581+
return;
15831582
}
15841583

1585-
this.SyncSelectedItems(sender as IList, this.PART_PopupListBox?.SelectedItems, e);
1584+
this.SyncSelectedItems(sender as IList, this.PART_PopupListBox.SelectedItems, e);
15861585
}
15871586

15881587
private void SyncSelectedItems(IList? sourceCollection, IList? targetCollection, NotifyCollectionChangedEventArgs e)
15891588
{
1590-
if (this.IsSyncingSelectedItems || sourceCollection is null || targetCollection is null)
1589+
if (this.IsSyncingSelectedItems || sourceCollection is null || targetCollection is null || !this.IsInitialized)
15911590
{
15921591
return;
15931592
}

src/MahApps.Metro/Themes/MultiSelectionComboBox.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
</Border.Background>
2323
<DockPanel LastChildFill="True">
2424
<Button VerticalAlignment="Bottom"
25-
Command="mah:MultiSelectionComboBox.ClearContentCommand"
25+
Command="{x:Static mah:MultiSelectionComboBox.ClearContentCommand}"
2626
Content="{x:Static lang:MultiSelectionComboBox.ResetTextToSelectedItems}"
2727
DockPanel.Dock="Bottom" />
2828
<Path Margin="50"

0 commit comments

Comments
 (0)