的問題與你正在做的是,爲提取存儲在拖動裏面的數據什麼是零,您必須指定確切類型。
control.DoDragDrop(new Label(), DragDropEffects.Move);
e.Data.GetDataPresent(typeof(Control)) // = false
e.Data.GetDataPresent(typeof(Label)) // = true
你所要做的就是創建一個包裝並將其用於你的拖放代碼。
class ControlWrapper
{
public Control Control { get; private set; }
public ControlWrapper(Control control) { Control = control; }
}
control.DoDragDrop(new ControlWrapper(new Label()), DragDropEffects.Move);
e.Data.GetDataPresent(typeof(ControlWrapper)) // = true
現在你的代碼變得
ControlWrapper controlWrapper = e.Data.GetData(typeof(ControlWrapper)) as ControlWrapper;
UserControl userControl = controlWrapper.Control as UserControl;
,並試圖不健全的太說教,但如果你想先的形式存在的數據,你應該檢查。 e.Data.GetDataPresent(typeof(ControlWrapper))
會告訴你它是否是這種格式。
+1對於EXACT類型,謝謝 – wruckie 2014-04-01 21:58:39