对于 WPF TreeView,如何在设置 TreeView.ItemContainerStyle 的同时使用主题

发布于 2024-11-13 11:34:52 字数 594 浏览 8 评论 0原文

我在 XAML 中使用它从基于 Josh Smith 的示例代码 此处的视图模型加载树视图子级

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

这会导致我用于 TreeViewItem 的主题被忽略。它将所选项目的文本设为黑色,背景设为深蓝色,因此难以阅读。有没有办法同时使用主题和上面的代码?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫弦 2024-11-20 11:34:52

尝试设置 BaseOn{StaticResource {x:Type TreeViewItem}}

这会将 TreeViewItems 的默认样式(由主题提供)作为样式的基础。

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.

傾城如夢未必闌珊 2024-11-20 11:34:52

只是格式化的代码:

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文