Skip to content

Commit

Permalink
ListBoxAssist.IsToggle - Ensure last clicked item is always selected (#…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
nicolaihenriksen authored Oct 29, 2023
1 parent e670764 commit 4b14f71
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
4 changes: 2 additions & 2 deletions MainDemo.Wpf/Toggles.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,11 @@

<smtx:XamlDisplay HorizontalAlignment="Left" UniqueKey="buttons_63">
<!-- the following based on https://material.io/guidelines/components/buttons.html#buttons-toggle-buttons -->
<ListBox SelectedIndex="0" Style="{StaticResource MaterialDesignToolVerticalToggleListBox}">
<ListBox SelectedIndex="0" Style="{StaticResource MaterialDesignToolVerticalToggleListBox}" materialDesign:ListBoxAssist.CanUserToggleSelectedItem="True">
<ListBox.ToolTip>
<StackPanel>
<TextBlock Text="MaterialDesignToolToggleListBox" />
<TextBlock Text="Exclusive selection" />
<TextBlock Text="Exclusive selection (allows un-toggling selected item)" />
<TextBlock Text="ListBoxAssist.IsToggle allows more natural toggle behaviour" />
</StackPanel>
</ListBox.ToolTip>
Expand Down
4 changes: 2 additions & 2 deletions MaterialDesign3.Demo.Wpf/Toggles.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,11 @@

<smtx:XamlDisplay HorizontalAlignment="Left" UniqueKey="buttons_63">
<!-- the following based on https://material.io/guidelines/components/buttons.html#buttons-toggle-buttons -->
<ListBox SelectedIndex="0" Style="{StaticResource MaterialDesignToolVerticalToggleListBox}">
<ListBox SelectedIndex="0" Style="{StaticResource MaterialDesignToolVerticalToggleListBox}" materialDesign:ListBoxAssist.CanUserToggleSelectedItem="True">
<ListBox.ToolTip>
<StackPanel>
<TextBlock Text="MaterialDesignToolToggleListBox" />
<TextBlock Text="Exclusive selection" />
<TextBlock Text="Exclusive selection (allows un-toggling selected item)" />
<TextBlock Text="ListBoxAssist.IsToggle allows more natural toggle behaviour" />
</StackPanel>
</ListBox.ToolTip>
Expand Down
11 changes: 10 additions & 1 deletion MaterialDesignThemes.Wpf/ListBoxAssist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private static void ListBoxMouseButtonEvent(object sender, MouseButtonEventArgs

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

listBoxItem.SetCurrentValue(ListBoxItem.IsSelectedProperty, !listBoxItem.IsSelected);
listBoxItem.SetCurrentValue(ListBoxItem.IsSelectedProperty, !GetCanUserToggleSelectedItem(senderElement) || !listBoxItem.IsSelected);
mouseButtonEventArgs.Handled = true;

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

public static bool GetIsToggle(DependencyObject element)
=> (bool)element.GetValue(IsToggleProperty);

public static readonly DependencyProperty CanUserToggleSelectedItemProperty = DependencyProperty.RegisterAttached(
"CanUserToggleSelectedItem", typeof(bool), typeof(ListBoxAssist), new FrameworkPropertyMetadata(default(bool)));

public static void SetCanUserToggleSelectedItem(DependencyObject element, bool value)
=> element.SetValue(CanUserToggleSelectedItemProperty, value);

public static bool GetCanUserToggleSelectedItem(DependencyObject element)
=> (bool)element.GetValue(CanUserToggleSelectedItemProperty);
}

0 comments on commit 4b14f71

Please sign in to comment.