Search This Blog

Tuesday, October 29, 2013

Handle Mouse Click Event on TreeViewItem TreeView WPF

WPF > ItemsControl > TreeView > Events > MoseLeft

How to capture Left Mouse Click on TreeView item control in WPF?

Use ItemContainerStyle and EventSetter



Example:

XAML

<TreeView x:Name="tvMain>
           <TreeView.ItemContainerStyle>
             <Style TargetType="{x:Type TreeViewItem}">
                       <EventSetter Event="MouseLeftButtonUp" Handler="treeViewItem_MouseLeftButtonUp"/>
              </Style>
       </TreeView.ItemContainerStyle>
</TreeView>

c# Code:

private void treeViewItem_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
      TreeViewItem tvi = (TreeViewItem)sender;
      e.Handled = true;
}