我正在尝试在WPF中为ImageControl实现DragAndDrop。我使用C#codebehind将图像添加到网格中。
System.Windows.Controls.Image OldMan = new System.Windows.Controls.Image();
OldMan.Height=30;
OldMan.Width=30;
OldMan.Name="OldMan";
OldMan.Margin=new Thickness(100,100,0,0);
OldMan.HorizontalAlignment=System.Windows.HorizontalAlignment.Left;
OldMan.Stretch = Stretch.Fill;
OldMan.VerticalAlignment = System.Windows.VerticalAlignment.Top;
OldMan.Source= ConvertBitmap(Properties.Resources.Old1);
OldMan.MouseDown += new MouseButtonEventHandler(OldMan_MouseDown);
//PW is the name of my Grid
PW.Children.Add(OldMan);
PW.RegisterName(OldMan.Name, OldMan);
这会将Image添加到网格中,并挂钩到MouseDown事件
MouseDown事件中的
void OldMan_MouseDown(object sender, MouseButtonEventArgs e)
{
System.Windows.Controls.Image img = (System.Windows.Controls.Image)sender;
DoDragDrop(.... //this doesn't exist so obviously I am missing something
}
所以我没有DoDragDrop选项,所以我的选项是什么
答案 0 :(得分:2)
WPF教程上有一篇可能有用的文章:
http://wpftutorial.net/DragAndDrop.html
它的基础是当他们按下鼠标时,你记录起点。然后当他们移动鼠标时(没有释放鼠标按钮),你移动对象的数量与他们移动鼠标的数量相同。