wpf - MouseDoubleClick gets wrong TreeViewItem -
this 1 weird unless i'm missing basic.
i have attached event handler treeviewitem
's mousedoubleclick
event through itemcontainerstyle
:
<treeview.itemcontainerstyle> <style targettype="treeviewitem"> <eventsetter event="mousedoubleclick" handler="treeviewitem_mousedoubleclick" /> </style> </treeview.itemcontainerstyle>
here's event handler:
private sub treeviewitem_mousedoubleclick(sender object, e mousebuttoneventargs) if typeof sender treeviewitem dim tvi = directcast(sender, treeviewitem) msgbox(tvi.header) end if end sub
the problem msgbox
shows header text of root node, not node on double-clicked. can't see obvious mistake here. can point me in right direction?
yup, you're missing 1 of basic weird things treeview :). not sure how can illustrate in post, i'll try explain it. treeviewitem has sub items. when expand root treeviewitem show sub items, sub treeviewitems inside of parent treeviewitem. when double click on child treeviewitem event bubble top item. if @ e.originalsource, you'll actual item... sort of... you'll object double clicked on (for example textblock). can use known getvisualancestor<t>()
extension method chase correct treeviewitem:
((frameworkelement)e.originalsource).getvisualancestor<treeviewitem>()
yeah, it's ugly :)...
here link c# implementation, you'll have find vb version :), pretty trivial port.
Comments
Post a Comment