如何拖动;将图像从包装面板拖放到画布上

时间:2014-07-22 07:11:09

标签: c# wpf canvas drag-and-drop wrappanel

我正在使用Visual Studio 2012编写C#WPF应用程序。

我从数据库中获取了一些图像并将它们放入包装面板中。从包装面板,我想拖动图像并将它们放入画布。这就是我从数据库中获取图像的方式:

private void GetImages(int taskID, int activityID)
{
   IList<ModelSQL.TwoCategory> ImagesList = twocat.GetImageURL(taskid, type);

   foreach (ModelSQL.TwoCategory tc in ImagesList)
   {
      var uriSource = new Uri(root.SetupInformation.ApplicationBase + tc.Pictures);
      Image img = new Image();
      img.Source = new BitmapImage(uriSource);
      img.VerticalAlignment = VerticalAlignment.Center;
      img.HorizontalAlignment = HorizontalAlignment.Center;
      img.Stretch = Stretch.Uniform;
      img.Height = 100;
      img.Width = 100;
      img.AllowDrop = true;
      img.PreviewMouseDown += new MouseButtonEventHandler(img_MouseDown);
      img.PreviewMouseMove += new MouseEventHandler(img_MouseMove);
      img.PreviewGiveFeedback += new GiveFeedbackEventHandler(img_GiveFeedback);
      img.PreviewMouseUp += new MouseButtonEventHandler(img_PreviewMouseUp);
      Canvas1.AllowDrop = true;
      Canvas2.AllowDrop = true;

      //Canvas1.PreviewDrop += new DragEventHandler(Canvas1_PreviewDrop);
      //Canvas2.PreviewDrop += new DragEventHandler(Canvas2_PreviewDrop);

      imgPanel.Children.Add(img);    
   }   
}

我使用拖动装饰器来拖动图像并且工作正常。

private void img_MouseDown(object sender, MouseButtonEventArgs e)
{
   startPoint = e.GetPosition(this);
}

private void img_MouseMove(object sender, MouseEventArgs e)
{
   // int y = 0, z = 0;
   if (e.LeftButton == MouseButtonState.Pressed)
   {
      var source = sender as UIElement;
      Image img = sender as Image;

      //img.Background = System.Windows.Media.Brushes.YellowGreen;
      Point current = e.GetPosition(this);
      Vector diff = startPoint - current;

      if (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
          Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)
      {    
         adorner = new DragAdorner(img, e.GetPosition(img));
         AdornerLayer.GetAdornerLayer(img).Add(adorner);

         var dragData = new DataObject(this);

         //string str = img.Content.ToString();    

         //DragDrop.DoDragDrop(lbl, lbl.Content, DragDropEffects.Move | DragDropEffects.Copy);
         DragDrop.DoDragDrop(img, dragData, DragDropEffects.Move | DragDropEffects.Copy);
         AdornerLayer.GetAdornerLayer(img).Remove(adorner);    
      }    
   }
}

private void img_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{ }

private void img_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
   if (adorner != null)
   {
      Image img = sender as Image;

      var pos = img.PointFromScreen(GetMousePosition());
      adorner.UpdatePosition(pos);
      e.Handled = true;
   }

}

[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;
};

public static Point GetMousePosition()
{
   Win32Point w32Mouse = new Win32Point();
   GetCursorPos(ref w32Mouse);
   return new Point(w32Mouse.X, w32Mouse.Y);
}

但是我在将图像放入画布时遇到了麻烦,而且我能够将图像拖到应用程序的任何位置。如何禁用它?

This是GUI

这是DROP EVENT

private void Canvas1_Drop(object sender, DragEventArgs e)
        {
            ImageSource imageSource = e.Data.GetData(typeof(ImageSource)) as ImageSource;
            Image img = sender as Image;
            img.Source = imageSource;
            Canvas Canvas1 = sender as Canvas;
            Canvas1.Children.Add(img);

        }

1 个答案:

答案 0 :(得分:0)

 DragDrop.DoDragDrop(img, dragData, DragDropEffects.Move | DragDropEffects.Copy);

您管理过,数据已经提供给拖放操作。

现在,每隔一个控制,例如如果他们想要/可以对数据做任何事情,画布可以决定他们的DragOver和Drop事件。 数据源或多或少地脱离了游戏,当拖放开始时,它就是完成剩下的目标。 如果您不希望数据离开源控件,则应附加到其DragLeave事件并取消拖放操作(如果鼠标移开)。