Search code examples
c#wpftreeviewitem

TreeView with HierarchicalDataTemplate, Is there a way to access the TreeViewItem events like MouseDoubleClick and ItemSelected?


I made a TreeView with HierarchicalDataTemplate. Is there a way to access the TreeViewItem events like MouseDoubleClick() and ItemSelected().

Here is my code:

<HierarchicalDataTemplate DataType="{x:Type local:Artist}" ItemsSource="{Binding Albums}">
    <TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>

Solution

  • What you want to do is set an ItemContainerStyle and then use EventSetters

    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <EventSetter Event="TreeViewItem.MouseDoubleClick"
                         Handler="TreeViewItem_MouseDoubleClick"/>
            <EventSetter Event="TreeViewItem.Selected"
                         Handler="TreeViewItem_Selected" />
    
    ....