Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}