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.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);
    }

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

4

1 回答 1

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;
    }
于 2013-06-11T07:01:08.090 回答