[英]drag and drop one image over other image in WPF
我正在嘗試將一個圖像(樹視圖中的該圖像)移動到另一圖像。 使用以下處理程序
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.