我需要从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 添加新的图像控件。
您应该简单地分配 img1
的 Source
属性:
img1.Source = (ImageSource)e.Data.GetData(typeof(ImageSource));
关于c# - 图像拖放(WPF 窗口),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36112599/