在WPF中使用拖放进行继承的问题

efl*_*les 2 wpf drag-and-drop c#-4.0

我有一个usercontrol,我想实现一个拖放界面,这是实现的重要部分,这工作正常:

要使用户控件的XML文件可拖动:

<UserControl 
         ...default xmlns...
         MouseLeftButtonDown="Control_MouseLeftButtonDown">
         ...GUI-ELEMENTS in the control...
</UserControl>
Run Code Online (Sandbox Code Playgroud)

代码背后:

public partial class DragableControl : UserControl
{
    private void Control_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
         DragDrop.DoDragDrop(this, this, DragDropEffects.Move);
    }
}
Run Code Online (Sandbox Code Playgroud)

XML文件到usercontrol,它将能够接受拖放操作:

<Usercontrol 
         ...default xmlns...>
    <Grid AllowDrop="True" Drop="Grid_Drop">
         ... GUI elements in the grid....
    </Grid> 
</Usercontrol>
Run Code Online (Sandbox Code Playgroud)

代码背后:

public partial class DropClass: UserControl
{
    private void Grid_Drop(object sender, DragEventArgs e)
    {
        var control = (DragableControl)e.Data.GetData(typeof(DragableControl));
        if(control != null)
        {
            //do something
        }        
    }
}
Run Code Online (Sandbox Code Playgroud)

为了能够创建具有拖放功能的不同用户控件,我创建了一个基类BaseDragableUserControl,它目前不包含任何东西,但是继承自usercontrol.

码:

public class BaseDragableUserControl: UserControl
{
}
Run Code Online (Sandbox Code Playgroud)

我改变了我的代码(xaml和代码):

 public partial class DragableControl : UserControl
Run Code Online (Sandbox Code Playgroud)

我也将接收的类更改为:

public partial class DropClass: UserControl
{
    private void Grid_Drop(object sender, DragEventArgs e)
    {
        var control =(BaseDragableUserControl)e.Data.GetData(typeof(BaseDragableUserControl));
        if(control != null)
        {
            //do something
        }        
    }
}
Run Code Online (Sandbox Code Playgroud)

控制变量始终为空.我想DragEventsArgs中的getdata不喜欢继承.有没有办法实现这个目标?为了能够使用拖放类的基类?

Ric*_*key 5

而不是this在启动拖放时传递,而是为此目的创建一个标准容器.特别:

DragDrop.DoDragDrop(this, new DataObject("myFormat", this), DragDropEffects.Move);
Run Code Online (Sandbox Code Playgroud)

然后现在你知道期望一种特定的数据:

var control =(BaseDragableUserControl)e.Data.GetData("myFormat");
Run Code Online (Sandbox Code Playgroud)