Problem Binding Commands to TreeViewItem using MVVMLight

2.8k Views Asked by At

I'm trying to use MVVMLight to bind a TreeViewItem Selected event to a command.

The TreeViewItem's are defined in a HierarchicalDataTemplate so I cannot add Interaction.Triggers (as shown below)

<HierarchicalDataTemplate 
            x:Key="TreeViewItemTemplate"
            ItemsSource="{Binding ChildReportViewModels}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Selected">
                    <MvvmLight_Command:EventToCommand Command="{Binding LoadReportCommand, Mode=OneWay}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
</HierarchicalDataTemplate>

How else can I add the EventTrigger to each TreeViewItem?

Thanks.

2

There are 2 best solutions below

0
On BEST ANSWER

I forgot about this question.

For future ref, here's the solution I used...

Instead of trying to bind the EventToCommand to the Selected event of the TreeView, I bound it to the MouseLeftButtonUpEvent of the TextBlock declared in the HierarchicalDataTemplate for TreeViewItems.

<HierarchicalDataTemplate 
   x:Key="TreeViewItemTemplate"
   ItemsSource="{Binding ChildReportViewModels}"
   ItemContainerStyle="{StaticResource TreeViewItemContainerStyle}">
   <StackPanel Orientation="Horizontal">        
      <TextBlock Text="{Binding Name}">
         <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseLeftButtonUp">
               <gs_cmd:EventToCommand Command="{Binding LoadPublicationCommand, Mode=OneWay}" CommandParameter="{Binding}" />
            </i:EventTrigger>
         </i:Interaction.Triggers>
      </TextBlock>
   </StackPanel>
</HierarchicalDataTemplate>
0
On

I have not much knowledge about MVVMLight and especially about EventTrigger.

But since there is no answer to your qestion yet the codeplex article TreeViewWithViewModel might help. It shows how to bind to SelectedItem and IsExpanded Properties in a wpf-treeview and how these can be used to implement load on demand in the treeview.