Display the context menu for first-level itemsĬontextMenu contextMenu = tvi.FindResource("FirstLevelContextMenu") as ContextMenu LastClickedItem.Background = Brushes.White If (lastClickedItem != null & !lastClickedItem.Equals(tvi)) If (e.RightButton = MouseButtonState.Pressed) If so, you can refer to the following code.Ĭodebehind: private TreeViewItem lastClickedItem = null You could check if the effect below is what you want. If (data != DependencyProperty.UnsetValue) return data If (data = DependencyProperty.UnsetValue) element = VisualTreeHelper.GetParent(element) as UIElement While (data = DependencyProperty.UnsetValue)ĭata = (element) Object data = DependencyProperty.UnsetValue UIElement element = source.InputHitTest(point) as UIElement Private static object GetDataFromSourceControl(ItemsControl source, Point point) If (data != null) DragDrop.DoDragDrop(dragSource, data, DragDropEffects.Move) Object data = GetDataFromSourceControl(dragSource, e.GetPosition(AssociatedObject)) TreeViewSource = new ObservableCollection() ListBoxSource = new ObservableCollection() Private void DeleteViewNodeFromSource(ObservableCollection viewItems, ViewItem viewItem)ĭeleteViewNodeFromSource(vi.ViewItems, viewItem) Private static T FindAnchestor(DependencyObject current) where T : DependencyObjectĬurrent = VisualTreeHelper.GetParent(current) Private void tv_DragLeave(object sender, DragEventArgs e) reset background on leaved possible target TreeViewItem If (tvi != null) tvi.Background = Brushes.White reset background on target TreeViewItem (viewItemToInsert) Įlse if (!targetItem.Equals(draggedTVI.DataContext as ViewItem))ĭeleteViewNodeFromSource(vm.TreeViewSource, data) ViewItem viewItemToInsert = new ViewItem(data.ItemName) ViewItem data = (ViewItem)e.Data.GetData(typeof(ViewItem)) ViewItem targetItem = (e.OriginalSource as TextBlock)?.DataContext as ViewItem If (!e.Data.GetDataPresent(typeof(ViewItem))) return MainViewModel vm = (sender as TreeView).DataContext as MainViewModel Private void Tv_Drop(object sender, DragEventArgs e) If (tvi != null) tvi.Background = Brushes.MediumPurple TreeViewItem tvi = FindAnchestor((DependencyObject)e.OriginalSource) Private void tv_DragOver(object sender, DragEventArgs e) ViewItem dragData = draggedTVI.DataContext as ViewItem ĭragDrop.DoDragDrop(draggedTVI, dragData, DragDropEffects.Move) Private void tv_MouseMove(object sender, MouseEventArgs e) start Drag&Drop when mouse is moved and there's a saved TreeViewItem Private void PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) Private void AssociatedObject_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)ĭraggedTVI = FindAnchestor((DependencyObject)e.OriginalSource) ĭraggedTVI.Background = Brushes.MediumPurple Public class TreeViewDragDropBehavior : BehaviorĪssociatedObject.PreviewMouseLeftButtonDown += PreviewMouseLeftButtonDown ĪssociatedObject.MouseMove += tv_MouseMove ĪssociatedObject.DragOver += tv_DragOver ĪssociatedObject.DragLeave += tv_DragLeave ĪssociatedObject.PreviewMouseRightButtonDown += AssociatedObject_PreviewMouseRightButtonDown Only when I move mouse out of the window and move back into the window, I can do operation again, but only one operation and the same issue appears again. Not to show ContextMenu on other TreeViewItem or not to collapse a node. Clicks on any other places will not show the ContextMenu.Īnother issue I am facing is, after I right click on a TreeViewItem to show the ContextMenu, I cannot perform any other operations on the whole window. As you can see in the screeshot, I only want the ContextMenu to be displayed when mouse right clicks on the first level TreeViewItem(those in yellow). Following MVVM, I have developed a TreeView and now I want mouse right click to show ContextMenu on certain TreeViewItem.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |