拖入wpf

时间:2018-08-29 09:01:47

标签: c# wpf drag-and-drop adorner

我在装饰者的支持下进行拖放控制。从右向左拖动列表项时,装饰器窗口不在鼠标指针附近。请任何人帮助我将装饰器窗口放在鼠标指针附近。我遵循以下使用以下问题创建装饰器窗口并显示装饰器的步骤 Please refer the question。我这样尝试过将装饰窗口放置在鼠标指针附近。但是它不能用于更高分辨率的窗口,我没有得到任何正确的解决方案。请提出您的想法。

行为

private Window _dragdropWindow = null;
ListBoxItem draggedItem = null;
Win32Point w32Mouse = new Win32Point();      

this.AssociatedObject.PreviewMouseMove += (sender, e) =>
{
   if (e.LeftButton == MouseButtonState.Pressed && dataObject != null && !IsDragging)
   {
      var currentPoint = e.GetPosition(sender as UIElement);
      if (Math.Abs(currentPoint.X - startingPoint.X) > 10 || (Math.Abs(currentPoint.Y - startingPoint.Y) > 10))
      {
         IsDragging = true;

         if (draggedItem != null)
         {
            CreateDragDropWindow(draggedItem);
         }

         DragDrop.DoDragDrop(sender as ListBox, dataObject, DragDropEffects.Copy);

         if (_dragdropWindow != null)
         {
            _dragdropWindow.Close();
            _dragdropWindow = null;
         }
      }
   }    
};


this.AssociatedObject.PreviewMouseLeftButtonDown += (sender, e) =>
{
   var listBoxItem = VisualHelper.FindParentOfType(e.OriginalSource as DependencyObject, typeof(ListBoxItem)) as ListBoxItem;
   if (listBoxItem != null)
   {
      startingPoint = e.GetPosition(sender as UIElement);
      dataObject = listBoxItem.DataContext as Details;                                       
      draggedItem = VisualHelper.FindParentOfType(e.OriginalSource as DependencyObject, typeof(ListBoxItem)) as ListBoxItem;
   }
   else
   {
      dataObject = null;
      IsDragging = false;
   }
};

this.AssociatedObject.PreviewGiveFeedback += (sender, e) =>
{
   Win32Point w32Mouse = new Win32Point();   
   GetCursorPos(ref w32Mouse);               

  _dragdropWindow.Left = w32Mouse.X;
  _dragdropWindow.Top = w32Mouse.Y;                
}; 

 private void CreateDragDropWindow(Visual dragElement)
 {
    this._dragdropWindow = new Window();
    _dragdropWindow.WindowStyle = WindowStyle.None;
    _dragdropWindow.AllowsTransparency = true;
    _dragdropWindow.AllowDrop = false;
    _dragdropWindow.Background = null;
    _dragdropWindow.IsHitTestVisible = false;
    _dragdropWindow.SizeToContent = SizeToContent.WidthAndHeight;
    _dragdropWindow.Topmost = true;
    _dragdropWindow.ShowInTaskbar = false;

    Rectangle rectangle = new Rectangle();
    rectangle.Width = ((FrameworkElement)dragElement).ActualWidth;
    rectangle.Height = ((FrameworkElement)dragElement).ActualHeight;
    rectangle.Fill = new VisualBrush(dragElement);
    this._dragdropWindow.Content = rectangle;           

    this._dragdropWindow.Left = w32Mouse.X;
    this._dragdropWindow.Top = w32Mouse.Y;
    this._dragdropWindow.Show();
 }


[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetCursorPos(ref Win32Point pt);

[StructLayout(LayoutKind.Sequential)]
internal struct Win32Point
{
   public Int32 X;
   public Int32 Y;
};

拖动列表项时,装饰器不在鼠标指针附近Please refer the screenshot

0 个答案:

没有答案