I was able to do this by creating my own ItemContainerStyle for the TreeView. You should consider this along with the following code snippets.
1. ViewModel for TreeView
public class TreeViewModelView { public IEnumerable<object> Values { get { } }
2. Define a TreeView and configure mandatory data binding:
<TreeView x:Name="lstItems" HorizontalAlignment="Left" Margin="21,19,0,80" Width="283" DataContext="{DynamicResource TreeViewModelView}" ItemsSource="{Binding Values, Mode=OneWay}" ItemTemplate="{DynamicResource TreeViewDataTemplate}" ItemContainerStyle="{DynamicResource TreeViewItemStyle}"> <TreeView.Resources> <mv:TreeViewModelView x:Key="TreeViewModelView" /> <HierarchicalDataTemplate x:Key="TreeViewDataTemplate" ItemsSource="{Binding Items}"> <Grid> <TextBlock Text="{Binding Name}" /> </Grid> </HierarchicalDataTemplate> </TreeView.Resources> </TreeView>
3. In Expression Blend, you can create an editing copy of the ItemContainerStyle template
<Style x:Key="TreeViewItemStyle" TargetType="{x:Type TreeViewItem}"> ... </Style>
4. Then replace ToggleButton with:
<ToggleButton x:Name="Expander" ClickMode="Press" IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ExpandCollapseToggleStyle}"> <i:Interaction.Triggers> <i:EventTrigger EventName="Checked"> <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding Path=DataContext.ExpandedCommand, RelativeSource={RelativeSource AncestorType={x:Type TreeView}}, Mode=OneWay}" CommandParameter="{Binding}" /> </i:EventTrigger> <i:EventTrigger EventName="Unchecked"> <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding Path=DataContext.CollapsedCommand, RelativeSource={RelativeSource AncestorType={x:Type TreeView}}, Mode=OneWay}"/> </i:EventTrigger> </i:Interaction.Triggers> </ToggleButton>
source share