Skip to content

Commit 4b14f71

Browse files
ListBoxAssist.IsToggle - Ensure last clicked item is always selected (#3349)
* Ensures last clicked item is always selected * Add attached property to allow toggling of selected item Default behavior is now changed so the selected item can not be deselected. Setting this new AP will allow it, basically falling back to the old default behavior.
1 parent e670764 commit 4b14f71

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

MainDemo.Wpf/Toggles.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,11 @@
338338

339339
<smtx:XamlDisplay HorizontalAlignment="Left" UniqueKey="buttons_63">
340340
<!-- the following based on https://material.io/guidelines/components/buttons.html#buttons-toggle-buttons -->
341-
<ListBox SelectedIndex="0" Style="{StaticResource MaterialDesignToolVerticalToggleListBox}">
341+
<ListBox SelectedIndex="0" Style="{StaticResource MaterialDesignToolVerticalToggleListBox}" materialDesign:ListBoxAssist.CanUserToggleSelectedItem="True">
342342
<ListBox.ToolTip>
343343
<StackPanel>
344344
<TextBlock Text="MaterialDesignToolToggleListBox" />
345-
<TextBlock Text="Exclusive selection" />
345+
<TextBlock Text="Exclusive selection (allows un-toggling selected item)" />
346346
<TextBlock Text="ListBoxAssist.IsToggle allows more natural toggle behaviour" />
347347
</StackPanel>
348348
</ListBox.ToolTip>

MaterialDesign3.Demo.Wpf/Toggles.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,11 @@
336336

337337
<smtx:XamlDisplay HorizontalAlignment="Left" UniqueKey="buttons_63">
338338
<!-- the following based on https://material.io/guidelines/components/buttons.html#buttons-toggle-buttons -->
339-
<ListBox SelectedIndex="0" Style="{StaticResource MaterialDesignToolVerticalToggleListBox}">
339+
<ListBox SelectedIndex="0" Style="{StaticResource MaterialDesignToolVerticalToggleListBox}" materialDesign:ListBoxAssist.CanUserToggleSelectedItem="True">
340340
<ListBox.ToolTip>
341341
<StackPanel>
342342
<TextBlock Text="MaterialDesignToolToggleListBox" />
343-
<TextBlock Text="Exclusive selection" />
343+
<TextBlock Text="Exclusive selection (allows un-toggling selected item)" />
344344
<TextBlock Text="ListBoxAssist.IsToggle allows more natural toggle behaviour" />
345345
</StackPanel>
346346
</ListBox.ToolTip>

MaterialDesignThemes.Wpf/ListBoxAssist.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ private static void ListBoxMouseButtonEvent(object sender, MouseButtonEventArgs
3232

3333
if (listBoxItem is null || !listBoxItem.IsEnabled) return;
3434

35-
listBoxItem.SetCurrentValue(ListBoxItem.IsSelectedProperty, !listBoxItem.IsSelected);
35+
listBoxItem.SetCurrentValue(ListBoxItem.IsSelectedProperty, !GetCanUserToggleSelectedItem(senderElement) || !listBoxItem.IsSelected);
3636
mouseButtonEventArgs.Handled = true;
3737

3838
listBoxItem.Focus();
@@ -55,4 +55,13 @@ public static void SetIsToggle(DependencyObject element, bool value)
5555

5656
public static bool GetIsToggle(DependencyObject element)
5757
=> (bool)element.GetValue(IsToggleProperty);
58+
59+
public static readonly DependencyProperty CanUserToggleSelectedItemProperty = DependencyProperty.RegisterAttached(
60+
"CanUserToggleSelectedItem", typeof(bool), typeof(ListBoxAssist), new FrameworkPropertyMetadata(default(bool)));
61+
62+
public static void SetCanUserToggleSelectedItem(DependencyObject element, bool value)
63+
=> element.SetValue(CanUserToggleSelectedItemProperty, value);
64+
65+
public static bool GetCanUserToggleSelectedItem(DependencyObject element)
66+
=> (bool)element.GetValue(CanUserToggleSelectedItemProperty);
5867
}

0 commit comments

Comments
 (0)