Search code examples
c#wpftreeviewprismeventtrigger

MVVM wpf TreeView how to handle Expanded event


I'm implementing a delayed load treeview, and need to handle the Expanded event. I would like to do it directly in the ViewModel. I have hooked up an EventTrigger:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Expanded">
        <interactivity:InvokeCommandAction Command="{Binding HandleExpandCmd}" TriggerParameterPath="OriginalSource"  />
    </i:EventTrigger>  
</i:Interaction.Triggers>

While this works as expected, the OriginalSource is a TreeViewItem, and I would prefer to not include System.Windows.Controls in my ViewModel.
Alternatively, I could handle the event in code-behind, and pass the TreeViewItems's DataContext to the ViewModel. (The DataContext, of course, is what the ViewModel needs to work with.)
Is there a syntax I can use in the TriggerParameterPath that passes the TVI's DataContext, instead of the TVI itself?
Thanks ---


Solution

  • I would suggest to bind the TreeViewItem's IsExpanded property to one in your ViewModel. Then you can handle it in the ViewModel however you want!

    Here is an example of how to do that.