[英]Image drag and drop (WPF Window)
我需要從Image
img2
拖放到Image
img1
。 這意味着我想在將img2
拖動到img1
時復制它。 計划正在啟動,但目標Image
img1
之后我拖不改變Image
img2
就可以了。
如何解決這個問題使其發揮作用?
我的代碼如下:
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;
不會向畫布添加新的圖像控件。
相反,您應該簡單地分配img1
的Source
屬性:
img1.Source = (ImageSource)e.Data.GetData(typeof(ImageSource));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.