开发者

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜