简体   繁体   English

图像拖放(WPF窗口)

[英]Image drag and drop (WPF Window)

I need to drag and drop from Image img2 to Image img1 . 我需要从Image img2拖放到Image img1 That means that I want to copy img2 when I drag it on img1 . 这意味着我想在将img2拖动到img1时复制它。 Program is starting, but destination Image img1 is not changing after I drag Image img2 on it. 计划正在启动,但目标Image img1之后我拖不改变Image img2就可以了。

How to solve this problem to make it work ? 如何解决这个问题使其发挥作用?

My code below: 我的代码如下:

XAML: XAML:

<Canvas Name="va">
        <Image Name="img1" Height="100" Width="100" AllowDrop="True" Drop="img1_Drop" />
        <Image Name="img2" Height="100" Width="100" Source="Resources/eye.bmp" 
               MouseLeftButtonDown="img2_MouseLeftButtonDown" AllowDrop="True" />
</Canvas>

C# code: C#代码:

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

private void img1_Drop(object sender, DragEventArgs e)
{
    Image imageControl = (Image)sender;
    if ((e.Data.GetData(typeof(ImageSource)) != null))
    {
        ImageSource image = e.Data.GetData(typeof(ImageSource)) as ImageSource;
        imageControl = new Image() { Width = 100, Height = 100, Source = image };
        img1 = imageControl;
    }
}

Assigning img1 = imageControl; 分配img1 = imageControl; will not add a new Image control to the Canvas. 不会向画布添加新的图像控件。

You should instead simply assign the Source property of img1 : 相反,您应该简单地分配img1Source属性:

img1.Source = (ImageSource)e.Data.GetData(typeof(ImageSource));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM