Reputation: 969
I have a WPF TreeView and I want to remove the blue "selected" hover on the left side of the icon. I hope you understand what I want :D
Heres a picture of the problem
The TreeView is populated by code behind and I build the Item programmatically.
The TreeViewItem.Header XAML would look like this:
<StackPanel Orientation="Horizontal" ...>
<Image ... />
<TextBlock .../>
</StackPanel>
Upvotes: 1
Views: 656
Reputation: 38144
It is like a workaround and it is not good solution:
<StackPanel Orientation="Horizontal" Margin="-1,0,0,0">
<Image Source="/Images/yourImage.jpg" Height="30" Width="30"/>
<TextBlock Text="Hey"/>
</StackPanel>
However, it is better to use HierarchicalDataTemplate
:
<TreeView ItemsSource="{Binding Leafs}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type vm:LeafViewModel}" ItemsSource="{Binding Children}">
<ContentControl Content="{Binding }">
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate" Value="{StaticResource DefaultTemplate}"/>
</Style>
</ContentControl.Style>
</ContentControl>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
and its HierarchicalDataTemplate
:
<HierarchicalDataTemplate x:Key="DefaultTemplate" DataType="{x:Type vm:LeafViewModel}" ItemsSource="{Binding Children}">
<Border Tag="{Binding DataContext, RelativeSource={RelativeSource Self}}" Background="Transparent">
<StackPanel Orientation="Horizontal">
<Label VerticalAlignment="Center" FontFamily="WingDings" Content="1"/>
<CheckBox IsChecked="{Binding IsCheckedFoo}"/>
<TextBlock Name="leafTxtBox" Text="{Binding LeafName}" Tag="{Binding DataContext, RelativeSource={RelativeSource Self}}" Background="Transparent"/>
</StackPanel>
</Border>
</HierarchicalDataTemplate>
The best article about how to use TreeView
without violating MVVM rules is by Josh Smith.
Upvotes: 1
Reputation: 120
There are 4 brushes that generally correspond to this and are used by the default Template for TreeViewItem
keys:
HighlightBrushKey - Background with focus.
HighlightTextBrushKey - Foreground with focus.
InactiveSelectionHighlightBrushKey - Background without focus.
InactiveSelectionHighlightTextBrushKey - Foreground without focus.
Just override them as you see fit, for your requirement something like this would do fine:
<TreeView>
<TreeView.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}"
Color="Black" />
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}"
Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}"
Color="Black" />
</TreeView.Resources>
</TreeView>
Upvotes: 0
Reputation: 35713
TreeView wraps items into TreeViewItem container. TreeViewItem has Padding 1,0,0,0
which can be removed via Style
<TreeView.Resources>
<Style TargetType="TreeViewItem">
<Setter Property="Padding" Value="0"/>
</Style>
</TreeView.Resources>
Upvotes: 0