Skip to content

Commit

Permalink
Improve ClearTextButton behaviour for MultiSelectionComboBox
Browse files Browse the repository at this point in the history
  • Loading branch information
timunie committed Aug 13, 2021
1 parent 13a3ab8 commit 48fab02
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/MahApps.Metro/Controls/Helper/TextBoxHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ public static void ButtonClicked(object sender, RoutedEventArgs e)
{
if (multiSelectionComboBox.HasCustomText)
{
multiSelectionComboBox.ResetEditableText();
multiSelectionComboBox.ResetEditableText(true);
}
else
{
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,15 +532,15 @@ public bool InterceptMouseWheelSelection
/// <summary>
/// Resets the custom Text to the selected Items text
/// </summary>
public void ResetEditableText()
public void ResetEditableText(bool forceUpdate = false)
{
if (this.PART_EditableTextBox is not null)
{
var oldSelectionStart = this.PART_EditableTextBox.SelectionStart;
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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -916,7 +914,7 @@ private static void ExecutedClearContentCommand(object sender, ExecutedRoutedEve
{
if (multiSelectionCombo.HasCustomText)
{
multiSelectionCombo.ResetEditableText();
multiSelectionCombo.ResetEditableText(true);
}
else
{
Expand All @@ -933,6 +931,7 @@ private static void ExecutedClearContentCommand(object sender, ExecutedRoutedEve
throw new NotSupportedException("Unknown SelectionMode");
}
}
multiSelectionCombo.ResetEditableText(true);
}
}

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}
}

Expand All @@ -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);
}
}

Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/MahApps.Metro/Themes/MultiSelectionComboBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</Border.Background>
<DockPanel LastChildFill="True">
<Button VerticalAlignment="Bottom"
Command="mah:MultiSelectionComboBox.ClearContentCommand"
Command="{x:Static mah:MultiSelectionComboBox.ClearContentCommand}"
Content="{x:Static lang:MultiSelectionComboBox.ResetTextToSelectedItems}"
DockPanel.Dock="Bottom" />
<Path Margin="50"
Expand Down

0 comments on commit 48fab02

Please sign in to comment.