public virtual void ProcessMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            if (e.Handled)
            {
                return;
            }

#if DEBUG
            this.CheckMouseEventArgs(e);
#endif

            if (!this.DraggedElement.CaptureMouse())
            {
                Debug.Fail("The DragSourceManager was unable to capture the mouse on the dragged element.");
                return;
            }

            //Get the current window (which can be a popup) on which the drag is happening
            var parent = this.GetDraggedElementWindow();

            this.InitialMousePositionToDragContainerAdorner = e.GetPosition(this.AdornerLayerInsideDragContainer);
            this.InitialMousePositionToDraggedElement       = e.GetPosition(this.DraggedElement);
            this.MouseToScreenPositionFactor = DragSourceManager.CalculateMouseToScreenPositionFactor(parent);
            this.ParentWindowIsPopup         = (parent is Popup);

            var draggedElementMousePosition = e.GetPosition(this.DraggedElement);
            m_draggedElementMouseOffset = new Vector(draggedElementMousePosition.X, draggedElementMousePosition.Y);
        }
        private static Vector CalculateMouseToScreenPositionFactor(FrameworkElement element)
        {
            if (element == null)
            {
                return(new Vector(1d, 1d));
            }

            var source = PresentationSource.FromVisual(element);

            if (source == null)
            {
                var parent = DragSourceManager.GetPopupParent(element);
                while (parent != null)
                {
                    source = PresentationSource.FromDependencyObject(parent);
                    if (source != null)
                    {
                        break;
                    }

                    parent = DragSourceManager.GetPopupParent(parent);
                }
            }

            double x, y;

            if (source != null)
            {
                var deviceUnits = source.CompositionTarget.TransformToDevice;
                x = deviceUnits.M11;
                y = deviceUnits.M22;
            }
            else
            {
                using (var hwnd = new HwndSource(new HwndSourceParameters()))
                {
                    var deviceUnits = hwnd.CompositionTarget.TransformToDevice;
                    x = deviceUnits.M11;
                    y = deviceUnits.M22;
                }
            }

            return(new Vector((x == 0d) ? 1d : 1d / x, (y == 0d) ? 1d : 1d / y));
        }
예제 #3
0
    private void SetupDragManager()
    {
      // We do not support DragDrop when there are no AdornerLayer because there wouldn't 
      // be any visual feedback for the operation.
      if( AdornerLayer.GetAdornerLayer( this ) == null )
        return;

      DataGridContext dataGridContext = DataGridControl.GetDataGridContext( this );

      DataGridControl dataGridControl = ( dataGridContext != null )
        ? dataGridContext.DataGridControl
        : null;

      // Can be null in design-time (edition of a style TargetType GroupByItem).
      if( dataGridControl == null )
        return;

      Debug.Assert( m_dragSourceManager == null, "There might be problems when there is already a m_dragSourceManager." );

      if( m_dragSourceManager != null )
      {
        m_dragSourceManager.PropertyChanged -= new PropertyChangedEventHandler( m_dragSourceManager_PropertyChanged );
        m_dragSourceManager.DragOutsideQueryCursor -= new QueryCursorEventHandler( m_dragSourceManager_DragOutsideQueryCursor );
        m_dragSourceManager.DroppedOutside -= new EventHandler( m_dragSourceManager_DroppedOutside );
      }

      // The DataGridControl's AdornerDecoratorForDragAndDrop must be used for dragging in order to include the 
      // RenderTransform the DataGridControl may performs. This AdornerDecorator is defined in the ControlTemplate
      // as PART_DragDropAdornerDecorator
      if( ( dataGridControl.DragDropAdornerDecorator != null )
          && ( dataGridControl.DragDropAdornerDecorator.AdornerLayer != null ) )
      {
        m_dragSourceManager = new DragSourceManager( this, dataGridControl.DragDropAdornerDecorator.AdornerLayer, dataGridControl );
      }
      else
      {
        System.Diagnostics.Debug.Assert( false, "The drag and drop functionnality won't be fully working properly: PART_DragDropAdornerDecorator was not found" );
        m_dragSourceManager = new DragSourceManager( this, null, dataGridControl );
      }

      m_dragSourceManager.PropertyChanged += new PropertyChangedEventHandler( m_dragSourceManager_PropertyChanged );
      m_dragSourceManager.DragOutsideQueryCursor += new QueryCursorEventHandler( m_dragSourceManager_DragOutsideQueryCursor );
      m_dragSourceManager.DroppedOutside += new EventHandler( m_dragSourceManager_DroppedOutside );
    }