Skip to content

Commit 01abe27

Browse files
Add ShowSelection Property to ListBoxItem (#2387)
* Add ShowSelection property for ListBoxItem * Add example in Demo App For no selection highlight for ListBoxItem * Fix an error in DependencyProperty * Update MainDemo.Wpf/Lists.xaml yup, you're right Co-authored-by: campersau <[email protected]> Co-authored-by: campersau <[email protected]>
1 parent 7aa629b commit 01abe27

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

MainDemo.Wpf/Lists.xaml

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
77
xmlns:domain="clr-namespace:MaterialDesignDemo.Domain"
88
xmlns:smtx="clr-namespace:ShowMeTheXAML;assembly=ShowMeTheXAML"
9+
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
910
mc:Ignorable="d"
1011
d:DesignHeight="300"
1112
d:DesignWidth="300"
@@ -44,6 +45,7 @@
4445
<ColumnDefinition Width="1*"/>
4546
<ColumnDefinition Width="1*"/>
4647
<ColumnDefinition Width="1*"/>
48+
<ColumnDefinition Width="1*"/>
4749
</Grid.ColumnDefinitions>
4850
<smtx:XamlDisplay
4951
UniqueKey="list_1"
@@ -63,9 +65,28 @@
6365
IsChecked="True"
6466
Content="Enabled"/>
6567

68+
<smtx:XamlDisplay
69+
UniqueKey="list_4"
70+
Grid.Column="1">
71+
<Grid>
72+
<ListBox>
73+
<ListBox.ItemContainerStyle>
74+
<Style TargetType="ListBoxItem" BasedOn="{StaticResource MaterialDesignListBoxItem}">
75+
<Setter Property="materialDesign:ListBoxItemAssist.ShowSelection" Value="False"/>
76+
</Style>
77+
</ListBox.ItemContainerStyle>
78+
<TextBlock Text="Listbox"/>
79+
<TextBlock Text="Without"/>
80+
<TextBlock Text="Selection"/>
81+
<TextBlock Text="Highlights"/>
82+
</ListBox>
83+
</Grid>
84+
85+
</smtx:XamlDisplay>
86+
6687
<smtx:XamlDisplay
6788
UniqueKey="list_2"
68-
Grid.Column="1"
89+
Grid.Column="2"
6990
Grid.Row="0">
7091
<!-- piece together your own items control to create some nice stuff that will make everyone think you are cool. and rightly so, because you are cool. you might even be a hipster for all I know -->
7192
<ItemsControl
@@ -107,7 +128,7 @@
107128

108129
<smtx:XamlDisplay
109130
UniqueKey="list_3"
110-
Grid.Column="2"
131+
Grid.Column="3"
111132
Grid.Row="0">
112133
<!-- and here's another -->
113134
<ItemsControl
@@ -196,4 +217,3 @@
196217
</smtx:XamlDisplay>
197218
</Grid>
198219
</UserControl>
199-

MaterialDesignThemes.Wpf/ListBoxItemAssist.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,14 @@ public static readonly DependencyProperty CornerRadiusProperty
1818
public static void SetCornerRadius(DependencyObject element, CornerRadius value) => element.SetValue(CornerRadiusProperty, value);
1919
#endregion
2020

21+
#region ShowSelection
22+
public static bool GetShowSelection(DependencyObject element)
23+
=> (bool)element.GetValue(ShowSelectionProperty);
24+
public static void SetShowSelection(DependencyObject element, bool value)
25+
=> element.SetValue(ShowSelectionProperty, value);
26+
27+
public static readonly DependencyProperty ShowSelectionProperty =
28+
DependencyProperty.RegisterAttached("ShowSelection", typeof(bool), typeof(ListBoxItemAssist), new PropertyMetadata(true));
29+
#endregion
2130
}
2231
}

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.ListBox.xaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@
223223
<Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
224224
<Setter Property="Padding" Value="8"/>
225225
<Setter Property="SnapsToDevicePixels" Value="True"/>
226+
<Setter Property="wpf:ListBoxItemAssist.ShowSelection" Value="True"/>
226227
<Setter Property="Template">
227228
<Setter.Value>
228229
<ControlTemplate TargetType="{x:Type ListBoxItem}">
@@ -278,7 +279,8 @@
278279
<Border x:Name="SelectedBorder" Opacity="0"
279280
Background="{TemplateBinding Foreground, Converter={StaticResource BrushRoundConverter}}">
280281
</Border>
281-
<wpf:Ripple Feedback="{TemplateBinding Foreground, Converter={StaticResource BrushRoundConverter}}"
282+
<wpf:Ripple x:Name="Ripple"
283+
Feedback="{TemplateBinding Foreground, Converter={StaticResource BrushRoundConverter}}"
282284
Focusable="False"
283285
Content="{TemplateBinding Content}"
284286
ContentTemplate="{TemplateBinding ContentTemplate}"
@@ -294,6 +296,11 @@
294296
<Trigger Property="IsEnabled" Value="False">
295297
<Setter Property="Opacity" Value=".56" />
296298
</Trigger>
299+
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=(wpf:ListBoxItemAssist.ShowSelection)}" Value="False">
300+
<Setter TargetName="MouseOverBorder" Property="Visibility" Value="Collapsed"/>
301+
<Setter TargetName="SelectedBorder" Property="Visibility" Value="Collapsed"/>
302+
<Setter TargetName="Ripple" Property="Feedback" Value="Transparent"/>
303+
</DataTrigger>
297304
</ControlTemplate.Triggers>
298305
</ControlTemplate>
299306
</Setter.Value>

MaterialDesignThemes.Wpf/TreeViewAssist.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ public static void SetExpanderSize(DependencyObject element, double value)
126126
#region ShowSelection
127127

128128
public static bool GetShowSelection(DependencyObject element)
129-
=> (bool)element.GetValue(ExpanderSizeProperty);
129+
=> (bool)element.GetValue(ShowSelectionProperty);
130130
public static void SetShowSelection(DependencyObject element, bool value)
131-
=> element.SetValue(ExpanderSizeProperty, value);
131+
=> element.SetValue(ShowSelectionProperty, value);
132132

133133
public static readonly DependencyProperty ShowSelectionProperty =
134134
DependencyProperty.RegisterAttached("ShowSelection", typeof(bool), typeof(TreeViewAssist), new PropertyMetadata(true));

0 commit comments

Comments
 (0)