2016-09-27 117 views
0

2色の背景をカスタムツリービュー(wpf)に設定したいとします。この妥当性は、DataGridで設定できます。私はツリービューのためにそれを行う方法を理解できませんでした。2色の背景をTreeView(WPF)に設定する方法

また、要素の選択をセルと同じ大きさに設定したいと考えています。

<TreeView Grid.Row="2" Name="TreeView" DataContext="{Binding Path=TreeModel}" ItemsSource="{Binding TreeItems}" SelectedItemChanged="TreeView_OnSelectedItemChanged"> 
    <TreeView.Resources> 
     <HierarchicalDataTemplate DataType="{x:Type local:NodeViewModel}" ItemsSource="{Binding Children}"> 
      <StackPanel Orientation="Horizontal"> 
       <Label Content="{Binding NameNode}"/> 
      </StackPanel> 
     </HierarchicalDataTemplate> 
    </TreeView.Resources> 
    <TreeView.ItemContainerStyle> 
     <Style TargetType="{x:Type TreeViewItem}"> 
      <Setter Property="FontWeight" Value="Normal" /> 
      <Setter Property="ContextMenu"> 
       <Setter.Value> 
        <ContextMenu> 
         <MenuItem Header="Add" Command="{Binding AddMachinePart_Command}"/> 
         <MenuItem Header="Remove" Command="{Binding RemoveMachinePart_Command}" IsEnabled="{Binding IsModule}"/> 
         <MenuItem Header="Edit" Command="{Binding EditMachinePart_Command}" IsEnabled="{Binding IsModule}"/> 
         <Separator></Separator> 
         <MenuItem Header="Copy path" Command="{Binding CopyPath_Command}" IsEnabled="{Binding IsModule}"></MenuItem> 
        </ContextMenu> 
       </Setter.Value> 
      </Setter> 
      <Style.Triggers> 
       <Trigger Property="IsSelected" Value="True"> 
        <Setter Property="FontWeight" Value="Bold" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </TreeView.ItemContainerStyle> 
</TreeView> 

enter image description here

は編集:
https://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.alternationindex(v=vs.110).aspxからの溶液を用いて、のようなツリービューグリッドと同じくらい大きいことが理想的である

enter image description here

@jschroedl提案しました下の図に示すように:

enter image description here

+1

[WPF:ItemsControlでの交互の色の可能な複製](http://stackoverflow.com/questions/4404539/wpf-alternating-colors-on-a-itemscontrol) – MikeT

+0

ツリーノードを交互にしたい背景色は? – leetibbett

+0

@leetibbettはい。 –

答えて

1

おそらく、(あなたのケースでは2)のサイクルにどのように多くの色を制御するためにStyleItemsControl.AlternationIndexを使用したいと思うでしょう。

MSDN Docs for ItemsControl.AlternationIndexの例はListBoxであり、おそらくTreeViewにも対応しています。

+0

ありがとう、それはすぐにバットから働く!今、私はフル・ツリー・ビュー・セルを色付けするために何ができるかを考えています。 –

関連する問題