开发者

For WPF TreeView, how can I use a theme while also setting the TreeView.ItemContainerStyle

开发者 https://www.devze.com 2023-03-11 10:44 出处:网络
I use this in XAML to load the treeview children from a view model based on Josh Smith\'s sample code here:

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

精彩评论

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

关注公众号