Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WPF中,将一个图像拖放到另一个图像上_C#_Wpf_Drag And Drop_Drag - Fatal编程技术网

C# 在WPF中,将一个图像拖放到另一个图像上

C# 在WPF中,将一个图像拖放到另一个图像上,c#,wpf,drag-and-drop,drag,C#,Wpf,Drag And Drop,Drag,我正在尝试将一个图像(树状视图中的此图像)移动到另一个图像中。 使用以下处理程序 private void DragImage(object sender, MouseButtonEventArgs e) { Image image = e.Source as Image; DataObject data = new DataObject(typeof(ImageSource), image.Source); DragDrop.DoDra

我正在尝试将一个图像(树状视图中的此图像)移动到另一个图像中。 使用以下处理程序

 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);
    }
一旦我把图像放到画布上。它会坚持下去。我想再次在同一块画布上移动它。 你能建议如何实现吗??
提前感谢

通过代码中的一些更改解决了此问题

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

通过代码中的一些更改解决了此问题

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