Skip to content

Commit 893da40

Browse files
committed
fully working tool toggle style
1 parent 4d8e317 commit 893da40

File tree

2 files changed

+79
-30
lines changed

2 files changed

+79
-30
lines changed

MainDemo.Wpf/ProvingGround.xaml

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
<converters:EqualityToVisibilityConverter x:Key="EqualityToVisibilityConverter" />
2626

27-
<Style x:Key="MaterialDesignToggleListBoxItem" TargetType="{x:Type ListBoxItem}">
27+
<Style x:Key="MaterialDesignToolToggleListBoxItem" TargetType="{x:Type ListBoxItem}">
2828
<Setter Property="Background" Value="Transparent"/>
2929
<Setter Property="BorderThickness" Value="0"/>
3030
<Setter Property="BorderBrush" Value="{DynamicResource MaterialDesignDivider}"/>
@@ -60,13 +60,33 @@
6060
</VisualStateGroup>
6161
<VisualStateGroup Name="SelectionStates">
6262
<VisualStateGroup.Transitions>
63-
<VisualTransition GeneratedDuration="0:0:0.6"/>
63+
<VisualTransition From="*" To="Selected">
64+
<Storyboard>
65+
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="SelectedBackgroundBorder">
66+
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
67+
</DoubleAnimationUsingKeyFrames>
68+
</Storyboard>
69+
</VisualTransition>
70+
<VisualTransition From="*" To="SelectedUnfocused">
71+
<Storyboard>
72+
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="SelectedBackgroundBorder">
73+
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
74+
</DoubleAnimationUsingKeyFrames>
75+
</Storyboard>
76+
</VisualTransition>
77+
<VisualTransition From="Selected" To="Unselected">
78+
<Storyboard>
79+
<DoubleAnimation Storyboard.TargetName="SelectedBackgroundBorder"
80+
Storyboard.TargetProperty="Opacity"
81+
To="0" Duration="1"/>
82+
</Storyboard>
83+
</VisualTransition>
6484
</VisualStateGroup.Transitions>
6585
<VisualState Name="Selected">
6686
<Storyboard>
6787
<DoubleAnimation Storyboard.TargetName="SelectedBackgroundBorder"
6888
Storyboard.TargetProperty="Opacity"
69-
To="0.18" Duration="0"/>
89+
To="1" Duration="0"/>
7090
<DoubleAnimation Storyboard.TargetName="SelectedBorder"
7191
Storyboard.TargetProperty="Opacity"
7292
To="1" Duration="0"/>
@@ -75,27 +95,25 @@
7595
<VisualState Name="Unselected"/>
7696
<VisualState Name="SelectedUnfocused">
7797
<Storyboard>
98+
<DoubleAnimation Storyboard.TargetName="SelectedBackgroundBorder"
99+
Storyboard.TargetProperty="Opacity"
100+
To="1" Duration="0"/>
78101
<DoubleAnimation Storyboard.TargetName="SelectedBorder"
79102
Storyboard.TargetProperty="Opacity"
80-
To="0.03" Duration="0"/>
103+
To="1" Duration="0"/>
81104
</Storyboard>
82105
</VisualState>
83106
</VisualStateGroup>
84107
</VisualStateManager.VisualStateGroups>
85-
<Grid>
108+
<Grid Margin="0 0 -1 0">
86109
<Border x:Name="MouseOverBorder"
87110
Opacity="0"
88111
Background="{TemplateBinding Foreground, Converter={StaticResource BrushRoundConverter}}"/>
89112
<Border x:Name="SelectedBackgroundBorder"
90113
Opacity="0"
91-
Background="{TemplateBinding Foreground, Converter={StaticResource BrushRoundConverter}}"
92-
RenderTransformOrigin="0.5,0.5">
93-
<Border.RenderTransform>
94-
<ScaleTransform ScaleX="1"/>
95-
</Border.RenderTransform>
96-
</Border>
97-
98-
<materialDesign:Ripple Feedback="{TemplateBinding Foreground, Converter={StaticResource BrushRoundConverter}}"
114+
Background="{DynamicResource MaterialDesignDivider}" />
115+
<materialDesign:Ripple x:Name="Ripple" Feedback="{TemplateBinding Foreground, Converter={StaticResource BrushRoundConverter}}"
116+
Opacity=".56"
99117
Focusable="False"
100118
Content="{TemplateBinding Content}"
101119
ContentTemplate="{TemplateBinding ContentTemplate}"
@@ -113,12 +131,17 @@
113131
BorderBrush="{DynamicResource MaterialDesignDivider}" />
114132
</Grid>
115133
</Border>
134+
<ControlTemplate.Triggers>
135+
<Trigger Property="IsSelected" Value="True">
136+
<Setter TargetName="Ripple" Property="Opacity" Value=".92" />
137+
</Trigger>
138+
</ControlTemplate.Triggers>
116139
</ControlTemplate>
117140
</Setter.Value>
118141
</Setter>
119142
</Style>
120143

121-
<Style x:Key="MaterialDesignToggleListBox" TargetType="{x:Type ListBox}">
144+
<Style x:Key="MaterialDesignToolToggleListBox" TargetType="{x:Type ListBox}">
122145
<Setter Property="Background" Value="Transparent"/>
123146
<Setter Property="BorderBrush" Value="Transparent"/>
124147
<Setter Property="BorderThickness" Value="0"/>
@@ -128,9 +151,9 @@
128151
<Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
129152
<Setter Property="ScrollViewer.PanningMode" Value="Both"/>
130153
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
131-
<Setter Property="materialDesign:ListBoxAssist.IsQuickUnselectEnabled" Value="True" />
154+
<Setter Property="materialDesign:ListBoxAssist.IsToggle" Value="True" />
132155
<Setter Property="VerticalContentAlignment" Value="Center"/>
133-
<Setter Property="ItemContainerStyle" Value="{StaticResource MaterialDesignToggleListBoxItem}"/>
156+
<Setter Property="ItemContainerStyle" Value="{StaticResource MaterialDesignToolToggleListBoxItem}"/>
134157
<Setter Property="ItemsPanel">
135158
<Setter.Value>
136159
<ItemsPanelTemplate>
@@ -171,7 +194,7 @@
171194
</UserControl.Resources>
172195

173196
<StackPanel Margin="8">
174-
<ListBox Style="{StaticResource MaterialDesignToggleListBox}">
197+
<ListBox Style="{StaticResource MaterialDesignToolToggleListBox}">
175198
<ListBoxItem>
176199
<materialDesign:PackIcon Kind="FormatAlignLeft"/>
177200
</ListBoxItem>
@@ -186,7 +209,7 @@
186209
</ListBoxItem>
187210
</ListBox>
188211

189-
<ListBox Style="{StaticResource MaterialDesignToggleListBox}"
212+
<ListBox Style="{StaticResource MaterialDesignToolToggleListBox}"
190213
SelectionMode="Extended"
191214
Margin="0 8 0 0">
192215
<ListBoxItem>
@@ -195,11 +218,13 @@
195218
<ListBoxItem>
196219
<materialDesign:PackIcon Kind="FormatItalic"/>
197220
</ListBoxItem>
198-
<ListBoxItem>
221+
<ListBoxItem x:Name="UnderlineCheckbox">
199222
<materialDesign:PackIcon Kind="FormatUnderline"/>
200223
</ListBoxItem>
201224
</ListBox>
202225

226+
<CheckBox IsChecked="{Binding ElementName=UnderlineCheckbox, Path=IsSelected}">Underline?</CheckBox>
227+
203228
</StackPanel>
204229

205230
</UserControl>

MaterialDesignThemes.Wpf/ListBoxAssist.cs

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,51 @@ public static class ListBoxAssist
1515
static ListBoxAssist()
1616
{
1717
EventManager.RegisterClassHandler(typeof (ListBox), UIElement.PreviewMouseLeftButtonDownEvent,
18-
new MouseButtonEventHandler(Target));
18+
new MouseButtonEventHandler(ListBoxMouseButtonEvent));
1919
}
2020

21-
private static void Target(object sender, MouseButtonEventArgs mouseButtonEventArgs)
21+
private static void ListBoxMouseButtonEvent(object sender, MouseButtonEventArgs mouseButtonEventArgs)
2222
{
23-
Point pt = mouseButtonEventArgs.GetPosition((UIElement)sender);
24-
//VisualTreeHelper.HitTest()
25-
//mouseButtonEventArgs.
23+
var senderElement = (UIElement) sender;
24+
25+
if (!GetIsToggle(senderElement)) return;
26+
27+
var point = mouseButtonEventArgs.GetPosition(senderElement);
28+
var result = VisualTreeHelper.HitTest(senderElement, point);
29+
30+
ListBoxItem listBoxItem = null;
31+
Ripple ripple = null;
32+
foreach (var dependencyObject in result.VisualHit.GetVisualAncestry().TakeWhile(_ => listBoxItem == null))
33+
{
34+
listBoxItem = dependencyObject as ListBoxItem;
35+
if (ripple == null)
36+
ripple = dependencyObject as Ripple;
37+
}
38+
39+
if (listBoxItem == null) return;
40+
41+
listBoxItem.SetCurrentValue(ListBoxItem.IsSelectedProperty, !listBoxItem.IsSelected);
42+
mouseButtonEventArgs.Handled = true;
43+
44+
if (ripple != null && listBoxItem.IsSelected)
45+
{
46+
ripple.RaiseEvent(new MouseButtonEventArgs(mouseButtonEventArgs.MouseDevice, mouseButtonEventArgs.Timestamp, mouseButtonEventArgs.ChangedButton)
47+
{ RoutedEvent = Control.PreviewMouseLeftButtonDownEvent, Source = ripple }
48+
);
49+
}
2650
}
2751

28-
public static readonly DependencyProperty IsQuickUnselectEnabledProperty = DependencyProperty.RegisterAttached(
29-
"IsQuickUnselectEnabled", typeof(bool), typeof(ListBoxAssist), new FrameworkPropertyMetadata(default(bool)));
52+
public static readonly DependencyProperty IsToggleProperty = DependencyProperty.RegisterAttached(
53+
"IsToggle", typeof(bool), typeof(ListBoxAssist), new FrameworkPropertyMetadata(default(bool)));
3054

31-
public static void SetIsQuickUnselectEnabled(DependencyObject element, bool value)
55+
public static void SetIsToggle(DependencyObject element, bool value)
3256
{
33-
element.SetValue(IsQuickUnselectEnabledProperty, value);
57+
element.SetValue(IsToggleProperty, value);
3458
}
3559

36-
public static bool GetIsQuickUnselectEnabled(DependencyObject element)
60+
public static bool GetIsToggle(DependencyObject element)
3761
{
38-
return (bool)element.GetValue(IsQuickUnselectEnabledProperty);
62+
return (bool)element.GetValue(IsToggleProperty);
3963
}
4064
}
4165
}

0 commit comments

Comments
 (0)