2017-09-07 8 views
0

내 응용 프로그램에서 여러 트 리뷰 컨트롤을 가지고 있고, 그들이이 모든 유사합니다 공유 특정 자원

<TreeView SelectedItemChanged="TreeView_OnSelectedItemChanged" x:Name="AccountsTree" > 
    <TreeView.Resources> 
     <!-- styles --> 
    </TreeView.Resources> 
    <TreeViewItem Header="Things" > 
     <TreeViewItem.Resources> 

      <!--From: https://stackoverflow.com/a/17814749/107037--> 
      <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue" /> 
      <!--<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />--> 
      <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Transparent" /> 
      <!--<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="Black" />--> 

      <HierarchicalDataTemplate DataType="{x:Type local:ThingEntity}" ItemsSource="{Binding Children, Mode=OneWay}"> 
       <HierarchicalDataTemplate.Resources> 
        <!-- styles --> 
       </HierarchicalDataTemplate.Resources> 
       <StackPanel Orientation="Horizontal"> 
        <i:Interaction.Behaviors> 
         <!-- drag 'n drop --> 
        </i:Interaction.Behaviors> 
        <Image x:Name="ThingIcon" /> 
        <TextBlock Text="{Binding ThingName}" Margin="6,0,6,0" /> 
       </StackPanel> 
      </HierarchicalDataTemplate> 
     </TreeViewItem.Resources> 
    </TreeViewItem> 
</TreeView> 

나는이 답변에 selectedItem가 색상을 변경할 수있는 간단한 방법을 발견 TreeView shows blue for selected item

SolidColorBrush 정의 모음을 캡슐화하여 필요로하는 모든 TreeView에서 쉽게 다시 사용할 수있게하는 방법이 있습니까? 또한

, 모든 TreeViewItem 컨트롤에 브러쉬의 모음을 적용 할 수있는 방법은 모든 TreeViewItem 컨트롤에 정의 된 스타일이 훨씬

<Style TargetType="{x:Type TreeViewItem}"> 
    <!-- style stuff --> 
</Style> 

처럼이 적용 대상이다?

감사합니다 -

답변

0

스타일이 Resources을 가질 수 있습니다. 쉬운.

<Style TargetType="{x:Type TreeViewItem}"> 
    <Style.Resources> 

     <!--From: https://stackoverflow.com/a/17814749/107037--> 
     <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue" /> 
     <!--<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />--> 
     <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Transparent" /> 
     <!--<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="Black" />--> 

     <HierarchicalDataTemplate DataType="{x:Type local:ThingEntity}" ItemsSource="{Binding Children, Mode=OneWay}"> 
      <HierarchicalDataTemplate.Resources> 
       <!-- styles --> 
      </HierarchicalDataTemplate.Resources> 
      <StackPanel Orientation="Horizontal"> 
       <i:Interaction.Behaviors> 
        <!-- drag 'n drop --> 
       </i:Interaction.Behaviors> 
       <Image x:Name="ThingIcon" /> 
       <TextBlock Text="{Binding ThingName}" Margin="6,0,6,0" /> 
      </StackPanel> 
     </HierarchicalDataTemplate> 
    </Style.Resources> 
</Style> 

이들도 중첩 할 수 있습니다. 스타일의 리소스에는 상위 스타일의 리소스를 사용할 수있는 다른 스타일이 포함될 수 있습니다.

<Style x:Key="RedBlueTreeView" TargetType="TreeView"> 
    <Style.Resources> 
     <SolidColorBrush x:Key="BlueBrush" Color="Red" /> 

     <Style TargetType="TreeViewItem"> 
      <Style.Resources> 
       <HierarchicalDataTemplate 
        DataType="{x:Type local:ThingEntity}" 
        ItemsSource="{Binding Children}" 
        > 
        <Label 
         Foreground="{StaticResource BlueBrush}" 
         Content="{Binding Stuff}" 
         /> 
       </HierarchicalDataTemplate> 
      </Style.Resources> 
     </Style> 
    </Style.Resources> 
</Style> 
+0

감사합니다. 매우 간단합니다. Number8