Skip to content

Commit 18e7a14

Browse files
Update Treeview to offer new options (#2359)
* Add two new properties to TreeView They control the expander size and the selection feedback * Showcase changes in demo app
1 parent 35707ac commit 18e7a14

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

MainDemo.Wpf/Trees.xaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,13 @@
151151
<TreeView
152152
Grid.Row="0"
153153
ItemsSource="{Binding MovieCategories}"
154-
MinWidth="220"
154+
MinWidth="220"
155155
SelectedItemChanged="TreeView_SelectedItemChanged">
156156
<TreeView.Resources>
157+
<Style TargetType="TreeViewItem" BasedOn="{StaticResource MaterialDesignTreeViewItem}">
158+
<Setter Property="materialDesign:TreeViewAssist.ExpanderSize" Value="32"/>
159+
<Setter Property="materialDesign:TreeViewAssist.ShowSelection" Value="False"/>
160+
</Style>
157161
<HierarchicalDataTemplate
158162
DataType="{x:Type domain:MovieCategory}"
159163
ItemsSource="{Binding Movies}">

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.TreeView.xaml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,11 @@
7070
<Setter Property="Focusable" Value="False"/>
7171
<Setter Property="Width" Value="16"/>
7272
<Setter Property="Height" Value="16"/>
73+
<Setter Property="Background" Value="Transparent"/>
7374
<Setter Property="Template">
7475
<Setter.Value>
7576
<ControlTemplate TargetType="{x:Type ToggleButton}">
76-
<Border Background="Transparent" Height="16" Width="16">
77+
<Border Background="{TemplateBinding Background}" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}">
7778
<VisualStateManager.VisualStateGroups>
7879
<VisualStateGroup x:Name="CheckStates">
7980
<VisualStateGroup.Transitions>
@@ -162,6 +163,8 @@
162163
<Setter Property="Padding" Value="8" />
163164
<Setter Property="FocusVisualStyle" Value="{StaticResource MaterialDesignTreeViewItemFocusVisual}"/>
164165
<Setter Property="Foreground" Value="{Binding Foreground, RelativeSource={RelativeSource AncestorType={x:Type TreeView}}}"/>
166+
<Setter Property="wpf:TreeViewAssist.ExpanderSize" Value="16"/>
167+
<Setter Property="wpf:TreeViewAssist.ShowSelection" Value="True"/>
165168
<Setter Property="Template">
166169
<Setter.Value>
167170
<ControlTemplate TargetType="{x:Type TreeViewItem}">
@@ -177,7 +180,7 @@
177180
<RowDefinition/>
178181
</Grid.RowDefinitions>
179182
<VisualStateManager.VisualStateGroups>
180-
<VisualStateGroup Name="CommonStates">
183+
<VisualStateGroup Name="CommonStates" >
181184
<VisualStateGroup.Transitions>
182185
<VisualTransition GeneratedDuration="0:0:0.3" To="Normal">
183186
<VisualTransition.GeneratedEasingFunction>
@@ -200,7 +203,7 @@
200203
<VisualStateGroup.Transitions>
201204
<VisualTransition GeneratedDuration="0:0:0.6"/>
202205
</VisualStateGroup.Transitions>
203-
<VisualState Name="Selected">
206+
<VisualState Name="Selected" >
204207
<Storyboard>
205208
<DoubleAnimation Storyboard.TargetName="SelectedBorder"
206209
Storyboard.TargetProperty="Opacity"
@@ -261,8 +264,9 @@
261264
Style="{StaticResource MaterialDesignExpandCollapseToggleStyle}"
262265
Foreground="{TemplateBinding Foreground}"
263266
Margin="8 0 8 0"
264-
VerticalAlignment="Center"
265-
/>
267+
Width="{TemplateBinding wpf:TreeViewAssist.ExpanderSize}"
268+
Height="{TemplateBinding wpf:TreeViewAssist.ExpanderSize}"
269+
VerticalAlignment="Center"/>
266270

267271
<Border x:Name="MouseOverBorder"
268272
Grid.Column="1" Grid.ColumnSpan="2"
@@ -278,6 +282,7 @@
278282

279283
<Grid Grid.Column="1" Grid.ColumnSpan="2" x:Name="ContentGrid" Background="{TemplateBinding Background}">
280284
<wpf:Ripple Feedback="{TemplateBinding Foreground, Converter={StaticResource BrushRoundConverter}}"
285+
x:Name="Ripple"
281286
Focusable="False"
282287
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
283288
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
@@ -326,6 +331,11 @@
326331
<Trigger Property="IsEnabled" Value="false">
327332
<Setter Property="Opacity" Value=".56"/>
328333
</Trigger>
334+
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=(wpf:TreeViewAssist.ShowSelection)}" Value="False">
335+
<Setter TargetName="MouseOverBorder" Property="Visibility" Value="Collapsed"/>
336+
<Setter TargetName="SelectedBorder" Property="Visibility" Value="Collapsed"/>
337+
<Setter TargetName="Ripple" Property="Feedback" Value="Transparent"/>
338+
</DataTrigger>
329339
</ControlTemplate.Triggers>
330340
</ControlTemplate>
331341
</Setter.Value>

MaterialDesignThemes.Wpf/TreeViewAssist.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace MaterialDesignThemes.Wpf
99
{
1010
public static class TreeViewAssist
1111
{
12+
#region AdditionalTemplate
1213
/// <summary>
1314
/// Allows additional rendering for each tree node, outside of the rippled part of the node which responsds to user selection.
1415
/// </summary>
@@ -44,6 +45,10 @@ public static DataTemplate GetAdditionalTemplate(DependencyObject element)
4445
return (DataTemplate)element.GetValue(AdditionalTemplateProperty);
4546
}
4647

48+
#endregion
49+
50+
#region AdditionalTemplateSelector
51+
4752
/// <summary>
4853
/// Allows additional rendering for each tree node, outside of the rippled part of the node which responsds to user selection.
4954
/// </summary>
@@ -79,6 +84,10 @@ public static DataTemplateSelector GetAdditionalTemplateSelector(DependencyObjec
7984
return (DataTemplateSelector)element.GetValue(AdditionalTemplateSelectorProperty);
8085
}
8186

87+
#endregion
88+
89+
#region NoTemplate
90+
8291
private static readonly Lazy<DataTemplate> NoAdditionalTemplateProvider = new Lazy<DataTemplate>(CreateEmptyGridDataTemplate);
8392

8493
/// <summary>
@@ -99,5 +108,32 @@ public static DataTemplate CreateEmptyGridDataTemplate()
99108
return (DataTemplate)XamlReader.Load(memoryStream, parserContext);
100109
}
101110
}
111+
112+
#endregion
113+
114+
#region ExpanderSize
115+
116+
public static double GetExpanderSize(DependencyObject element)
117+
=> (double)element.GetValue(ExpanderSizeProperty);
118+
public static void SetExpanderSize(DependencyObject element, double value)
119+
=> element.SetValue(ExpanderSizeProperty, value);
120+
121+
public static readonly DependencyProperty ExpanderSizeProperty =
122+
DependencyProperty.RegisterAttached("ExpanderSize", typeof(double), typeof(TreeViewAssist), new PropertyMetadata(default(double)));
123+
124+
#endregion
125+
126+
#region ShowSelection
127+
128+
public static bool GetShowSelection(DependencyObject element)
129+
=> (bool)element.GetValue(ExpanderSizeProperty);
130+
public static void SetShowSelection(DependencyObject element, bool value)
131+
=> element.SetValue(ExpanderSizeProperty, value);
132+
133+
public static readonly DependencyProperty ShowSelectionProperty =
134+
DependencyProperty.RegisterAttached("ShowSelection", typeof(bool), typeof(TreeViewAssist), new PropertyMetadata(true));
135+
136+
#endregion
137+
102138
}
103139
}

0 commit comments

Comments
 (0)