Problems with treeview in WPF
-
Hi, I have made a custom class to populate a TreeView thru its ItemsSource. That class heritates ObservableCollection. The thing is that its nodes doesnt seem to be TreeViewItems because i cant apply code that works for normal treeviewitems. I'm talking about code to do right click select, etc. What i really want is to be able to automaticly select a node when i right click it, and then open the context menu. I can do this perfectly using the left button to select the node and then using the right click to access the context menu. The problem is that i cant use directly the right button which is something typical in windows enviorment. The code i'm using to achieve that is the following: Code Snippet tvNetworkExplorer.AddHandler(TreeViewItem.MouseDownEvent, new MouseButtonEventHandler(TvNetworkExplorer_MouseDown)); private void TvNetworkExplorer_MouseDown(object sender, MouseButtonEventArgs e) { TreeViewItem item = e.Source as TreeViewItem; if (e.RightButton == MouseButtonState.Pressed) { item.IsSelected = true; } } I think this happens because the items are not tree view items, as stated before, they are items from an observable collection of a custom type. How can i make such behaviour to happen? The problem is that even using the visual tree help i always get the first node. I need to show you the big picture. Maybe i have something really small failing: XAML:
-
Hi, I have made a custom class to populate a TreeView thru its ItemsSource. That class heritates ObservableCollection. The thing is that its nodes doesnt seem to be TreeViewItems because i cant apply code that works for normal treeviewitems. I'm talking about code to do right click select, etc. What i really want is to be able to automaticly select a node when i right click it, and then open the context menu. I can do this perfectly using the left button to select the node and then using the right click to access the context menu. The problem is that i cant use directly the right button which is something typical in windows enviorment. The code i'm using to achieve that is the following: Code Snippet tvNetworkExplorer.AddHandler(TreeViewItem.MouseDownEvent, new MouseButtonEventHandler(TvNetworkExplorer_MouseDown)); private void TvNetworkExplorer_MouseDown(object sender, MouseButtonEventArgs e) { TreeViewItem item = e.Source as TreeViewItem; if (e.RightButton == MouseButtonState.Pressed) { item.IsSelected = true; } } I think this happens because the items are not tree view items, as stated before, they are items from an observable collection of a custom type. How can i make such behaviour to happen? The problem is that even using the visual tree help i always get the first node. I need to show you the big picture. Maybe i have something really small failing: XAML:
I'm facing the same problem. I somehow managed to get it selected in the similar way and opened the contextmenu like TreeViewItem.ContextMenu.IsOpen=true; but the problem that occurred was, the commands bindings to the menu items(in xaml) don't work. :confused: