tmh
tmh

Reputation: 210

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 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?

Upvotes: 2

Views: 1019

Answers (2)

DTriix
DTriix

Reputation: 1

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>

Upvotes: 0

brunnerh
brunnerh

Reputation: 184647

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.

Upvotes: 3

Related Questions