2017-09-07 1 views
0

J'ai plusieurs contrôles TreeView dans ma demande, et ils sont tous semblables à ceci:partager certaines ressources entre TreeViewItem.Resources

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

j'ai trouvé le moyen simple de changer la couleur SelectedItem dans cette réponse: TreeView shows blue for selected item

Existe-t-il un moyen d'encapsuler la collection de définitions SolidColorBrush pour les rendre faciles à réutiliser dans tous les TreeViews qui en ont besoin?

Sinon, est-il un moyen d'appliquer cette collection de brosses à tous les contrôles de TreeViewItem, un peu comme

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

Applique le style défini à tous les contrôles de TreeViewItem?

Merci -

Répondre

0

Styles peuvent avoir Resources. Facile.

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

Vous pouvez également les imbriquer. Les ressources d'un style peuvent contenir un autre style, qui peut utiliser les ressources du style parent:

<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

Merci - très simple. Number8