protected Point GetPosition(UIElement relativeTo, FrameworkElement element, bool centered) { Point point = new Point(Canvas.GetLeft(element), Canvas.GetTop(element)); if(centered) point.Offset(element.Width / 2, element.Height / 2); return relativeTo.TranslatePoint(point, window); }
// Handler for element selection on the canvas providing resizing adorner void myCanvas_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { // Remove selection on clicking anywhere the window if (selected) { selected = false; if (selectedElement != null && aLayer.GetAdorners(selectedElement) != null) { // Remove the adorner from the selected element aLayer.Remove(aLayer.GetAdorners(selectedElement)[0]); selectedElement = null; } } //If adding a new link if (e.Source.GetType() == typeof(SystemBasicView)) { if (e.Source.GetType() == typeof(SystemBasicView) && _isAddNewLink) { if (!_isLinkStarted) { if (_link == null || _link.EndPoint != _link.StartPoint) { _isLinkStarted = true; _linkedSystem = e.Source as SystemBasicView; SystemViewModel svm = (_linkedSystem.DataContext) as SystemViewModel; _tempLink = new TemporaryConnectorViewModel(svm); _tempLink.StartPostion = _linkedSystem.Position; e.Handled = true; } } } // If a componentView is selected, add adorner layer to it else { _isDown = true; _startPoint = e.GetPosition(myCanvas); selectedElement = e.Source as UIElement; _originalPoint = selectedElement.TranslatePoint(new Point(0, 0), myCanvas); aLayer = AdornerLayer.GetAdornerLayer(selectedElement); aLayer.Add(new ResizingAdorner(selectedElement, myCanvas)); selected = true; e.Handled = true; } } }
public void AddDraggedColumnGhost( UIElement element ) { if( ( element == null ) || m_elementToDraggedElementAdorner.ContainsKey( element ) ) return; // Get the Rect for the DataGridControl DataGridControl dataGridControl = this.DraggedDataGridContext.DataGridControl; Rect dataGridControlRect = new Rect( 0, 0, dataGridControl.ActualWidth, dataGridControl.ActualHeight ); Point elementToDataGridControl = element.TranslatePoint( ColumnReorderingDragSourceManager.EmptyPoint, dataGridControl ); // Get the Rect for the element that request a ghost Rect elementRect = new Rect( elementToDataGridControl, element.RenderSize ); // This is a special case with the current Element that is always layouted, but can be out of view if( !elementRect.IntersectsWith( dataGridControlRect ) ) return; AnimatedDraggedElementAdorner adorner = new AnimatedDraggedElementAdorner( element, this.AdornerLayerInsideDragContainer, true ); this.ApplyContainerClip( adorner ); this.AdornerLayerInsideDragContainer.Add( adorner ); m_elementToDraggedElementAdorner.Add( element, adorner ); }