c# - 图像拖放(WPF 窗口)

标签 c# wpf xaml drag-and-drop

我需要从Image img2拖放到Image img1。这意味着当我将 img2 拖动到 img1 上时,我想复制它。程序正在启动,但在将 Image img2 拖到目标 Image img1 上后,目标 Image img1 没有改变。

How to solve this problem to make it work ?

我的代码如下:

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# 代码:

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

最佳答案

分配img1 = imageControl;不会向 Canvas 添加新的图像控件。

您应该简单地分配 img1Source 属性:

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

关于c# - 图像拖放(WPF 窗口),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36112599/

相关文章:

wpf - Popup 的 IsOpen 和 Visibility 属性之间的区别?

c# - MVC session 变量在 Localhost 上工作但不在实时服务器上工作?

c# - 中间层中异步 WCF 服务调用和将数据返回到 UI 层的质量设计

c# - 应用程序运行时动画 gif 不起作用

c# - 从 ViewModel 更改选定的选项卡并在 ViewModel 之间发送对象

c# - WPF MVVM 更改模型

c# - MDI 应用程序中的 "Partially modal"表单

c# - 如何将 SortableBindingList 分配给 LINQ 查询

c# - 将类处理程序限制为内容

.net - 将 UserControl 中子元素的焦点传递给 WPF 中的父元素