private void Toolbox_MouseMove(object sender, MouseEventArgs e) { if (!_draggingItem) { return; } // Get the current mouse position Point mousePosition = e.GetPosition(null); Vector diff = _mouseStartPosition - mousePosition; if (e.LeftButton == MouseButtonState.Pressed && (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance || Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)) { var listBoxItem = VisualTreeUtility.FindParent <ListBoxItem>( (DependencyObject)e.OriginalSource); if (listBoxItem == null) { return; } var itemViewModel = (ToolboxItemViewModel)_toolbox.ItemContainerGenerator. ItemFromContainer(listBoxItem); var dragData = new DataObject(ToolboxDragDrop.DataFormat, itemViewModel.Model); DragDrop.DoDragDrop(listBoxItem, dragData, DragDropEffects.Move); } }
private void Toolbox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { var listBoxItem = VisualTreeUtility.FindParent <ListBoxItem>( (DependencyObject)e.OriginalSource); _draggingItem = listBoxItem != null; _mouseStartPosition = e.GetPosition(_toolbox); }
/// <summary> /// Computes the coordinates, relative to the parent <see cref="GraphControl" />, of this connector. /// This is used to correctly position any connections that may be connected to this connector. /// (Say that 10 times fast.) /// </summary> private void UpdatePosition() { var parentGraphControl = VisualTreeUtility.FindParent <GraphControl>(this); if (parentGraphControl == null) { return; } var centerPoint = new Point(ActualWidth / 2, ActualHeight / 2); Position = TransformToAncestor(parentGraphControl).Transform(centerPoint); }
/* * protected override void OnDragEnter(DragEventArgs e) * { * e.Effects = ValidateDrag(e.OriginalSource, e) ? DragDropEffects.Copy : DragDropEffects.None; * e.Handled = true; * } * * protected override void OnDragOver(DragEventArgs e) * { * e.Effects = ValidateDrag(e.OriginalSource, e) ? DragDropEffects.Copy : DragDropEffects.None; * e.Handled = true; * } * * protected override void OnDrop(DragEventArgs e) * { * if (ValidateDrag(e.OriginalSource, e)) * OnDropAction(e.OriginalSource, e); * * e.Handled = true; * } * * private void OnPreviewMouseDown(object sender, MouseButtonEventArgs e) * { * UpdateSelection(e, false); * } * * private void OnPreviewMouseUp(object sender, MouseButtonEventArgs e) * { * _didDrag = false; * * if (_didSelect) * { * _didSelect = false; * return; * } * * if (e.OriginalSource != null) * UpdateSelection(e, true); * } * * private void OnPreviewMouseMove(object sender, MouseEventArgs e) * { * var mousePosition = e.GetPosition(null); * var diff = _mouseDownPosition - mousePosition; * * if (!_isDragging && e.LeftButton == MouseButtonState.Pressed && * (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance || * Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)) * { * var treeViewItem = VisualTreeUtility.FindParent<TreeViewItem>((DependencyObject)Mouse.DirectlyOver); * * if (treeViewItem == null) * return; * * if (_adornerEnabled) * { * var adornerBitmap = new RenderTargetBitmap((int)TreeView.ActualWidth, (int)TreeView.ActualHeight, 96, * 96, * PixelFormats.Pbgra32); * * var drawingVisual = new DrawingVisual(); * * using (var dc = drawingVisual.RenderOpen()) * { * VisualTreeUtility.Where(TreeView, visual => * { * var tvi = visual as TreeViewItem; * * if (tvi == null) return false; * if (!((CustomTreeItem)tvi.DataContext).IsSelected) return false; * * // get the border so children don't get drawn * var bd = (Border)VisualTreeUtility.First(tvi, x => (x as Border)?.Name == "Bd"); * var relativePos = bd.TranslatePoint(new Point(0, 0), TreeView); * * var rt = new RenderTargetBitmap((int)bd.ActualWidth * 2, (int)bd.ActualHeight, 96, 96, * PixelFormats.Pbgra32); * rt.Render(bd); * * dc.DrawImage(rt, new Rect(relativePos, new Size(bd.ActualWidth * 2, bd.ActualHeight))); * * return false; * }); * } * * adornerBitmap.Render(drawingVisual); * * _dragAdorner.Source = (DependencyObject)e.OriginalSource; * _dragAdorner.Image = adornerBitmap; * _dragAdorner.IsDragging = true; * _dragAdorner.Offset = e.GetPosition(TreeView); * * _adornerTimer.Start(); * } * * var dragData = GetDragDataFunc(); * DragDrop.DoDragDrop(treeViewItem, dragData, DragDropEffects.Copy | DragDropEffects.None); * * Debug.WriteLine("didDrag set to true"); * _didDrag = true; * } * } */ /// <summary> /// Updates the selection. /// </summary> /// <param name="e">Mouse button event argument.</param> /// <param name="allowSelectionClick"> /// If the clicked item is already selected, this argument determines if other items /// should be deselected. /// </param> private bool UpdateSelection(MouseButtonEventArgs e, bool allowSelectionClick) { var treeItemVisual = VisualTreeUtility.FindParent <TreeViewItem>((DependencyObject)e.OriginalSource); var treeItemViewModel = (CustomTreeItem)treeItemVisual?.DataContext; var clickPoint = e.GetPosition(TreeView); var controlDown = Keyboard.IsKeyDown(System.Windows.Input.Key.LeftCtrl); var shiftDown = Keyboard.IsKeyDown(System.Windows.Input.Key.LeftShift); /* * if (_didDrag) * { * Debug.WriteLine("DRAG CANCEL: _didDrag == true"); * _didDrag = false; * return; * }*/ if (!(treeItemVisual == null && shiftDown) && (treeItemVisual == null || shiftDown)) { Game.Selection.ClearSelection(); } if (treeItemVisual == null) { Debug.WriteLine("DRAG CANCEL: treeeItemVisual == null"); return(false); } if (e.ClickCount == 2 && e.ButtonState == MouseButtonState.Pressed) { var codeContainer = treeItemViewModel.Instance as LuaSourceContainer; if (codeContainer != null) { var doc = new CodeEditorViewModel(codeContainer); Editor.Current.Shell.OpenDocument(doc); codeContainer.Destroyed.Event += () => doc.TryClose(); } } if (treeItemViewModel.IsSelected && !allowSelectionClick) { Debug.WriteLine("DRAG CANCEL: (treeItemViewModel.IsSelected && !allowSelectionClick) == true"); return(false); } if (!controlDown && !shiftDown) { Game.Selection.ClearSelection(); } if (shiftDown && _lastSelected != null) { var needsReversed = clickPoint.Y < _lastPoint.Y; VisualTreeUtility.ForEach(TreeView, visual => { var tvi = visual as TreeViewItem; if (tvi == null) { return; } var relPoint = tvi.TranslatePoint(new Point(0, 0), TreeView); var check = needsReversed ? (relPoint.Y > clickPoint.Y && relPoint.Y < _lastPoint.Y) : (relPoint.Y <clickPoint.Y && relPoint.Y> _lastPoint.Y); if (check) { Game.Selection.Select(((CustomTreeItem)tvi.DataContext).Instance); } }); } if (shiftDown && _lastSelected != null) { Game.Selection.Select(_lastSelected.Instance); _didSelect = true; } if (controlDown) { if (treeItemViewModel.Instance.IsSelected) { Game.Selection.Deselect(treeItemViewModel.Instance); } else { Game.Selection.Select(treeItemViewModel.Instance); _didSelect = true; } } else { Game.Selection.Select(treeItemViewModel.Instance); _didSelect = true; } if (!shiftDown) { _lastSelected = treeItemViewModel; _lastPoint = clickPoint; } return(true); }
private static bool ParentIsDropDownScrollViewer(DependencyObject dependencyObject) { var parent = VisualTreeUtility.FindParent <ScrollViewer>(dependencyObject); return(parent != null && parent.Name == "DropDownScrollViewer"); }
private static bool ParentIsEditableListBoxControl(DependencyObject dependencyObject) { return(VisualTreeUtility.FindParent <EditableListBoxControl>(dependencyObject) != null); }