/// <summary> /// Applies the selection box on the view to know the selected items. /// </summary> private void ApplySelectionBox() { // Clear selection. this.ParentView.SelectedItems.Clear(); // Hidding the canvas hosting the selection box. this.SelectionBoxCanvas.Visibility = Visibility.Collapsed; this.SelectionBox.Visibility = Visibility.Collapsed; double lX = Canvas.GetLeft(this.SelectionBox); double lY = Canvas.GetTop(this.SelectionBox); double lWidth = this.SelectionBox.Width; double lHeight = this.SelectionBox.Height; Rect lSelectionRect = new Rect(lX, lY, lWidth, lHeight); // Inflate the drag selection-rectangle by 1/10 of its size to make sure the intended item is selected. lSelectionRect.Inflate(lWidth / 10, lHeight / 10); // Selecting the items contained in the selection rect. if (this.ParentView.ItemsSource != null) { foreach (IGraphItemViewModel lItem in this.ParentView.ItemsSource) { // Getting the corresponding container. AGraphItem lContainer = this.ParentView.GetContainerForViewModel(lItem); if (lContainer != null) { if (lSelectionRect.Contains(lContainer.BoundingBox)) { this.ParentView.SelectedItems.Add(lItem); } } } } }
/// <summary> /// Delegate called when the CanBeDragged property is modified. /// </summary> /// <param name="pSender">The item of interest.</param> /// <param name="pEventArgs">The event arguments.</param> private static void OnCanBeDraggedPropertyChanged(object pSender, DependencyPropertyChangedEventArgs pEventArgs) { AGraphItem lItem = pSender as AGraphItem; bool lCanBeDragged = Convert.ToBoolean(pEventArgs.NewValue); if (lItem != null) { if (lCanBeDragged) { lItem.MouseDown += OnItemMouseDown; lItem.MouseMove += OnItemMouseMove; lItem.MouseUp += OnItemMouseUp; } else { lItem.MouseDown -= OnItemMouseDown; lItem.MouseMove -= OnItemMouseMove; lItem.MouseUp -= OnItemMouseUp; } } }
/// <summary> /// Releases the dragged item as the drag process ends. /// </summary> private static void ReleaseDraggedItem() { if (sDraggedItem == null) { return; } SimpleGraphView lParentCanvas = sDraggedItem.FindVisualParent <SimpleGraphView>(); if (lParentCanvas == null) { return; } lParentCanvas.MouseMove -= OnParentCanvasMouseMove; lParentCanvas.MouseUp -= OnParentCanvasMouseUp; sDraggedItem.ReleaseMouseCapture(); sDraggedItem = null; sDragStartPos = null; }
/// <summary> /// Delegate called when the mouse is down on a graph view item. /// </summary> /// <param name="pSender">The item of interest.</param> /// <param name="pEventArgs">The event arguments.</param> private static void OnItemMouseDown(object pSender, MouseButtonEventArgs pEventArgs) { AGraphItem lItem = pSender as AGraphItem; if (lItem == null) { return; } SimpleGraphView lParentCanvas = lItem.FindVisualParent <SimpleGraphView>(); if (lParentCanvas == null) { return; } // Sometimes the mouse goes faster than the item. Its up to the canvas then to continue the drag lParentCanvas.MouseMove += OnParentCanvasMouseMove; lParentCanvas.MouseUp += OnParentCanvasMouseUp; sDraggedItem = lItem; sDragStartPos = pEventArgs.GetPosition(lItem); lItem.CaptureMouse(); }
/// <summary> /// Sets the property value for the given item. /// </summary> /// <param name="pItem">The item of interest.</param> /// <param name="pValue">Flag indicating if the item can be dragged.</param> public static void SetCanBeDragged(AGraphItem pItem, bool pValue) { pItem.SetValue(CanBeDraggedProperty, pValue); }
/// <summary> /// Returns the property value for the given item. /// </summary> /// <param name="pItem">The item of interest.</param> /// <returns>True if it can be dragged, false otherwise.</returns> public static bool GetCanBeDragged(AGraphItem pItem) { return((bool)pItem.GetValue(CanBeDraggedProperty)); }