【问题标题】:drag and drop one image over other image in WPF在 WPF 中将一张图像拖放到另一张图像上
【发布时间】:2013-06-10 12:47:23
【问题描述】:

我正在尝试将一张图片(树视图中的这张图片)移动到另一张图片中。 使用以下处理程序

 private void DragImage(object sender, MouseButtonEventArgs e)
    {
        Image image = e.Source as Image;
        DataObject data = new DataObject(typeof(ImageSource), image.Source);
        DragDrop.DoDragDrop(image, data, DragDropEffects.Copy);
    }

    private void DropImage(object sender, DragEventArgs e)
    {
        ImageSource image = e.Data.GetData(typeof(ImageSource)) as ImageSource;
        Image imageControl = new Image() { Width = 50, Height = 30, Source = image };

        Canvas.SetLeft(imageControl, e.GetPosition(this.Canvas).X);
        Canvas.SetTop(imageControl, e.GetPosition(this.Canvas).Y);
        this.Canvas.Children.Add(imageControl);
    }

一旦我将图像放在画布上。它会坚持下去。我想再次在同一个画布上移动它。 你能建议它如何实现吗? 提前致谢

【问题讨论】:

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


    【解决方案1】:

    通过修改代码解决了这个问题。

     private void DragImage(object sender, MouseButtonEventArgs e)
        {
            Image image = e.Source as Image;
            DataObject data = new DataObject(typeof(ImageSource), image.Source);
            DragDrop.DoDragDrop(image, data, DragDropEffects.All);
            moving = true;
        }
    
    
        private void DropImage(object sender, DragEventArgs e)
        {
            Image imageControl = new Image();
            if ((e.Data.GetData(typeof(ImageSource)) != null))
            {
                ImageSource image = e.Data.GetData(typeof(ImageSource)) as ImageSource;
                imageControl = new Image() { Width = 50, Height = 30, Source = image };
            }
            else
            {
                if ((e.Data.GetData(typeof(Image)) != null))
                {
                    Image image = e.Data.GetData(typeof(Image)) as Image;
                    imageControl = image;
                    if (this.Canvas.Children.Contains(image))
                    {
                        this.Canvas.Children.Remove(image);
                    }
                }
            }
    
            Canvas.SetLeft(imageControl, e.GetPosition(this.Canvas).X);
            Canvas.SetTop(imageControl, e.GetPosition(this.Canvas).Y);
            imageControl.MouseLeftButtonDown += imageControl_MouseLeftButtonDown;
            this.Canvas.Children.Add(imageControl);
    
        }
    
        void imageControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Image image = e.Source as Image;
            DataObject data = new DataObject(typeof(Image), image);
            DragDrop.DoDragDrop(image, data, DragDropEffects.All);
            moving = true;
        }
    

    【讨论】:

      猜你喜欢
      • 2014-07-07
      • 1970-01-01
      • 2019-04-22
      • 2011-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      相关资源
      最近更新 更多