![]() ![]() RadTreeViewItem item = (sender as RadContextMenu). In the RadContextMenu Opened() event handler I add the right clicked item to the RadTreeView SelectedItems collection if no other item is selected on a mouse right click (according to the requirement to allow multiple selection only through left mouse click + Ctrl key). I access all selected items, using the SelectedItems collection of the RadTreeView in the OnMenuItemClicked() event handler, in order to highlight them (since my ContextMenu items are all called Highlight :) ). In order to run the project you will need to set the 284486_TreeViewEmptyDisableContextMenu.Web as a StartUp Project and the 284486_TreeViewEmptyDisableContextMenuTestPage.aspx as a Start Page.Īlso, I may have not understood your scenario correctly. Please take a look at it and let me know if it works for you or if you need more info. ![]() I have prepared a sample project demonstrating this approach. ![]() If you need to clear the selection after the appropriate actions were performed on the selected items, you can use the RadTreeView.SelectedItems. Therefore, if you need to perform an action on the selected RadTreeViewItem, you will have to use the ContainerFromItemRecursive() method. 3 Answers Sorted by: 38 XAMLPlease keep in mind that if the RadTreeView is databound to a business object, the SelectedItems collection will consists of business objects. Then in the RadMenuItem Click() event handler you can perform an action on each selected item. Then, you can search through its parents until you find the RadTreeViewItem that was clicked and set its IsSelected property to true thus adding it to the RadTreeView SelectedItems collection. You can add a MouseRightButtonDown event handler and get the original object that raised the MouseRightButtonDown event (this can be done through the e.OriginalSource property). The node on which the context menu was opened should be selected as well. In the ViewModel, which is DataContext of the control (named RestoresTreeViewControl) is a command I want to bind one of the menu items to. I have added a HierarchicalDataTemplate for this TreeView and in that template is a ContextMenu for the Items. We need a way to find the selected nodes in a tree on right click to open the context menu for either single or multiple selection of the tree view item.Ģ. In that control there is a TreeView, which displays some items. In case of multiple selection, we have to select the nodes and we are not able to find the nodes which are selected while using the context menu.ġ. Return radContextMenu.GetClickedElement() } Private RadTreeViewItem ClickedTreeViewItem We have used the following piece of code to identify the node which was right clicked, and it gives us the correct value. The TreeView should have a context menu, which is specific for each object type. When we right click on the single node to open the context menu, the node which was right clicked does not get selected. A TreeView should be bound to a hierarchical object structure. The tree supports the multiple selection.Īs per our requirement, we need to find multiple selected tree nodes when the user right clicks on any one of the mulitple selected node to open the context menu. The tree has several levels of nodes and context menu is specific to the nodes at different levels. ![]() The tree and context menu are both dynamic and binds on the runtime. Source = VisualTreeHelper.We are using context menu with tree node. While (source != null & source is not TreeViewItem) Public static TreeViewItem? VisualUpwardSearch(DependencyObject? source) Var treeViewItem = VisualUpwardSearch(e.OriginalSource as DependencyObject) Private void MenuTreeView_OnPreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e) / Sets the selection of an item with the right click button before showing The following code example creates a basic TreeView and ContextMenuStrip associated with the root TreeNode of the TreeView. This has been updated with nullable annotations in mind. To be clear, the OnPreviewMouseRightButtonDown event goes on the TreeView object in your XAML. You can use this snippet to have it select the item the mouse is over before showing the context menu (just be sure to have appropriate null checks in place). The default behavior of a right click on a TreeView in WPF is that it shows the context menu if one is set but it does not select the item that the mouse is over. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |