Reputation: 9232
I want in the window of a wpf to set a treeview and make use of it through mouse events. Particularly, I would like a complex shape to be drawn on a canvas by a mouseclick on each item of the treeview. However I am not sure if the treeview can serve this purpose, since the implementation I made does not seem to work at all. The mouse events do not work (the delegates are not triggered). Could someone indicate me the reason? Have a look at the code:
<TreeView DockPanel.Dock="Left" Background="DarkGray" HorizontalAlignment="Left" VerticalAlignment="Top">
<TreeViewItem Header="Input Data">
<TreeViewItem Header="Vector" MouseEnter="create_Rhombus"/>
<TreeViewItem Header="Array"/>
</TreeViewItem>
<TreeViewItem Header="Maths">
<TreeViewItem Header="Add" MouseDown="create_AddRectangle"/>
<TreeViewItem Header="Subtract" MouseDown="create_SubtractRectangle"/>
<TreeViewItem Header="Multiply" MouseDown="create_MultiplyRectangle"/>
<TreeViewItem Header="Divide" MouseDown="create_DivideRectangle"/>
</TreeViewItem>
</TreeView>
The methods in the code behind file are:
private void create_AddRectangle(object sender, MouseButtonEventArgs e)
private void create_SubtractRectangle(object sender, MouseButtonEventArgs e)
….
However the above methods are not invoked. Why?
Upvotes: 0
Views: 1886
Reputation: 688
I think there might be a bug with using tunneling (ie PreviewMouseDown
). I implemented it and it fires correctly but if I try to access the DataContext I get a {DisconnectedItem}
object. From the reading I've done online, this appears to be a bug with WPF.
I did find that I can wire a MouseLeftButtonDown
and a MouseRightButtonDown
events for a single TreeViewItem
to the same method and it will work exactly the same way you'd expect the MouseDown
event to work.
Upvotes: 0