本文介绍了如何切换treeviewitem的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<window>
<window.resources>
<style x:Key="Style1" TargetType="TreeviewItem">
.
.
</Style>

<style x:Key="Style2" TargetType="TreeviewItem">
.
.
</Style>
</window.resources>
<grid>

<treeview x:Name="tree1">
<treeviweitem header="A" />
  <treeviweitem header="A1" />
  <treeviweitem header="A2" />
<treeviweitem header="B" />
</treeView>

<treeview x:Name="tree2"/>
<treeviewitem header="C" />
  <treeviewitem header="C1" />
  <treeviewitem header="C2" />
<treeviewitem header="D" />
</grid>
</window>







我有一个问题,希望得到你的帮助拜托!

我的问题是



1,如何设置tree1的所有treeviewitem(包括AA1 A2B)应用样式style1



2,如何设置tree2的所有treeviewitem(包括CC1 C2D)应用风格style2





我知道可以设置每个treeviewitem样式这个






I have a question, hope to get your help please!
my question is

1、how to set that all of treeviewitem of "tree1"(include "A" "A1" "A2" "B") to apply the style "style1"

2、how to set that all of treeviewitem of "tree2"(include "C" "C1" "C2" "D") to apply the style "style2"


I know can do that set each of treeviewitem style like this

<treeviewitem header="C" Style="{Staticresource style2}"/>
  <treeviewitem header="C1" Style="{Staticresource style2}"/>
  <treeviewitem header="C2" Style="{Staticresource style2}"/>
<treeviewitem header="D" Style="{Staticresource style2}"/>





,但这太愚蠢了(这需要设置每个listviewitem)



, but this is too stupid(this need set each of listviewitem)

推荐答案

<TreeView>
   <Treeview.resources>
     <Style Targettype="{x:Type TreeViewItem}">
            BasedOn="{StaticResource {x:Type TreeViewItem}}">
       <Setter Property="Background" Value="Green" />
     </Style>
   </Treeview.resources>
   <Treeviewitem header="Merry" />
   <Treeviewitem header="Christmas" />
</Treeview> 







<Grid.Resources>
   <ResourceDictionary>
     <Style x:Key="style" TargetType="{x:Type TreeViewItem}"

            BasedOn="{StaticResource {x:Type TreeViewItem}}">
            <Setter Property="Background" Value="Green" />            
            <Setter Property="ItemContainerStyle" Value="{Binding ItemContainerStyle, RelativeSource={RelativeSource AncestorType={x:Type TreeView}}}" />
     </Style>            
   </ResourceDictionary>
 </Grid.Resources>
  
 <TreeView ItemContainerStyle="{StaticResource style}">
   <TreeViewItem Header="Merry" >
     <TreeViewItem Header="Christmas" />
   </TreeViewItem>
 </TreeView>





问候

Joseph Leung



Regards
Joseph Leung


这篇关于如何切换treeviewitem的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 23:48