Search code examples
mvvmbindingcommandmvvm-lighttreeviewitem

Problem Binding Commands to TreeViewItem using MVVMLight


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.


Solution

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