WPF TreeView item pressed

I have an event issue in WPF. Say I have a basic data model and a tree view for representing data. The simplest thing I want to do is when I click on one element, I would do something with the underlying data associated with that element.

I tried to use the MouseLeftButtonDown event for a Textblock , but then the sending object is only a Textblock , and I cannot access the underlying data.

Now I also tried to use the MouseLeftButtonDown event for the TreeViewItem as follows:

 <TreeView.ItemContainerStyle> <Style TargetType="{x:Type TreeViewItem}"> <EventSetter Event="MouseLeftButtonDown" Handler="itemClicked"/> </Style> </TreeView.ItemContainerStyle> 

But I did not get a handler.

So how exactly should I do this? Is there any standard approach?

Thanks, advanced!

+4
source share
1 answer

The MouseLeftButtonDown event is a bubbling event that it handled somewhere in my route my choice of choice. You can use snoop to find out who handled the event. Using PreviewMouseLeftButtonDown / SelectedItemChanged or in your case MouseDoubleClick will solve the problem.

 <TreeView> <TreeView.ItemContainerStyle> <Style TargetType="{x:Type TreeViewItem}"> <EventSetter Event="MouseDoubleClick" Handler="itemDoubleClicked"/> </Style> </TreeView.ItemContainerStyle> </TreeView> 
+5
source

Source: https://habr.com/ru/post/1481141/


All Articles