For WPF TreeView, how can I use a theme while also setting the TreeView.ItemContainerStyle
I use this in XAML to load the treeview children from a view model based on Josh Smith's sample code here:
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
</Style>
</TreeView.ItemContainerStyle>
This causes the theme I'm using for TreeViewItem 开发者_JAVA技巧to be ignored. It makes the selected item text black and the background darkblue so it's hard to read. Is there a way to use both the theme and the code above at the same time?
Try setting BasedOn
to {StaticResource {x:Type TreeViewItem}}
.
This will take the default style for TreeViewItems
(which is provided by the theme) as the base for your style.
Just the code formated:
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource {x:TypeTreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
</Style>
</TreeView.ItemContainerStyle>
精彩评论