/// <summary> /// Initializes a new instance of the <see cref="RubberbandAdorner"/> class /// </summary> /// <param name="canvas">Parent canvas</param> /// <param name="dragStartPoint">Dragging start point</param> /// <param name="mode">Mode of selection rectangle</param> public RubberbandAdorner(CustomCanvas canvas, Point?dragStartPoint, SelectionRectnagleModes mode) : base(canvas) { this.canvas = canvas; this.startPoint = dragStartPoint; // choose color and selection behaviour based on mode switch (mode) { case SelectionRectnagleModes.Selection: { this.rubberandBrush = (Brush)Application.Current.FindResource("SelectionBrush"); this.toSelect = true; break; } case SelectionRectnagleModes.ChoiceBox: { this.rubberandBrush = (Brush)Application.Current.FindResource("MainItemsBrush"); this.toSelect = false; break; } case SelectionRectnagleModes.Grid: { this.rubberandBrush = (Brush)Application.Current.FindResource("MainItemsBrush"); this.toSelect = false; break; } } this.rubberbandPen = new Pen(this.rubberandBrush, 1); this.rubberbandPen.DashStyle = new DashStyle(new double[] { 2 }, 1); }
/// <summary> /// Handles mouse move on canvas /// </summary> /// <param name="e">The event args</param> protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if (e.LeftButton != MouseButtonState.Pressed) { this.dragStartPoint = null; } // add rubberband adorner if (this.dragStartPoint.HasValue) { AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(this); if (adornerLayer != null) { TemplateViewModel dataContext = (TemplateViewModel)this.DataContext; this.mode = SelectionRectnagleModes.Selection; if (dataContext.IsAddingChoiceBox) { this.mode = SelectionRectnagleModes.ChoiceBox; } else if (dataContext.IsAddingGrid) { this.mode = SelectionRectnagleModes.Grid; } else if (dataContext.IsAddingBarcode) { this.mode = SelectionRectnagleModes.Barcode; } else if (dataContext.IsAddingClipArea) { this.mode = SelectionRectnagleModes.Barcode; } RubberbandAdorner adorner = new RubberbandAdorner(this, this.dragStartPoint, this.mode); adorner.MouseUp += this.AdornerMouseUp; adornerLayer.Add(adorner); } e.Handled = true; } }