views:

56

answers:

1

I have the problem that parts of the TreeView Controltemplate need to change depending on the state of the bound (ItemsSource) ViewModels. For example the little expander icon needs to be exchanged with a different drawing based on each items ViewModel state. Additonally based on the state of each ViewModel the child items need to be arranged horizontally instead of the default vertically.

+1  A: 

Hi Bitbonk,

It sounds like you have to customize ItemsContainerStyle, not ControlTemplate. For example, if you want to mark TreeViewItem as selected whenever underlying ViewModel is selected, you can use the following style:

<TreeView ItemsSource="{Binding ...}
          ...>
 <TreeView.ItemContainerStyle>
   <Style TargetType="{x:Type TreeViewItem}">
     <!-- IsSelected is a property on ViewModel item -->
     <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
     <Setter .../>
   </Style>
 </TreeView.ItemContainerStyle>
</TreeView>

If Binding doesn't suit you, you can use Converters, Triggers in Style or ControlTemplate. Furthermore, you can also use triggers in DataTemplates.

PS: Wrote the code from head. May have a typo.

Anvaka
So I suppose I could completely exchange the ControlTemplate of the TreeViewItem Control in the ItemContainerStyle? This would be the only way to exchange that default expander icon (based on ViewModel state), right?
bitbonk
That's right, Bitbonk.
Anvaka