将样式应用于所有 TreeViewItem [英] Apply style to all TreeViewItem
问题描述
我遇到的问题是我有多个 TreeView 控件,每个 TreeView 都有自己的 TreeViewItem 样式,设置它
Hi The problem am having is that I have multiple TreeView control and each TreeView has its own TreeViewItem styles, setting it
TreeView ItemContainerStyle="{StaticResource Style1}"
TreeView ItemContainerStyle="{StaticResource Style1}"
将只设置根元素而不是所有子元素,如何将样式应用于 TreeView 中的所有子元素
will only set the root element NOT all the child elements, How to apply a style to all the child elements in a TreeView
推荐答案
有几种方法可以做到这一点:
There are several ways to accomplish this:
您可以将您的样式设为所有 TreeViewItems 的默认样式:
You could make your style the default for all TreeViewItems:
<Style TargetType="{x:Type TreeViewItem}">
...
</Style>
不同之处在于您没有设置 x:Key 属性,而是设置了 TargetType 属性.在这种情况下,您不需要在 TreeView 上设置 ItemContainerStyle.<小时>您还可以将您的样式设置为所有 TreeViewItems 的默认样式,但仅限于您的 TreeView:
The difference is that you do not set the x:Key attribute, but you do set the TargetType attribute. You do not need to set the ItemContainerStyle on your TreeView in this case.
You could also set your style as the default style for all TreeViewItems, but only within your TreeView:
<TreeView>
<TreeView.Resources>
<Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource Style1}"/>
</TreeView.Resources>
</TreeView>
在这种情况下,您也不需要在 TreeView 上设置 ItemContainerStyle.
In this case you also do not need to set the ItemContainerStyle on your TreeView.
你也可以改变你的风格如下
You could also alter your style as follows
<Style x:Key="Style1" TargetType="{x:Type TreeViewItem}">
<Setter Property="ItemContainerStyle" Value="{StaticResource Style1}"/>
</Style>
在这种情况下,您仍然需要在 TreeView 上设置 ItemContainerStyle.
In this case you'd still have to set the ItemContainerStyle on your TreeView.
这篇关于将样式应用于所有 TreeViewItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!