문제

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.

도움이 되었습니까?

해결책

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>

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top