首页 > 解决方案 > 如何使用锣在WPF中创建几个DropHandlers?

问题描述

我在我的项目中使用了一个龚框架。
我创建了以下 DropHandler。
xml:

<ListBox ItemsSource="{Binding Collection}" dd:DragDrop.IsDropTarget="True" dd:DragDrop.DropHandler="{Binding}"/>

视图模型:

class MyViewModel : IDropTarget
{
    ObservableCollection<SomeType> Collection;

    public void DragOver(IDropInfo dropInfo)
    {
        // ...
    }

    public void Drop(IDropInfo dropInfo)
    {
        // ...
    }
}

问题。我想和另一个 DropHandlerListBox在这个窗口中为另一个。但我不知道,我该怎么做。如何IDropTarget再次实现接口?

标签: c#wpfdrag-and-drop

解决方案


您不能在同一个类中“再次”实现接口,但可以将DropHandler属性绑定到IDropTarget视图模型的属性:

dd:DragDrop.DropHandler="{Binding FirstDropTarget}"

然后,您将创建一个新类来处理丢弃。如果您需要对视图模型的引用,您可以将其注入到IDropTarget实现中,例如:

class MyViewModel
{
    ObservableCollection<SomeType> Collection;

    public MyViewModel()
    {
        FirstDropTarget = new YourHandler(this);
        SecondDropTarget = new YourOtherHandler(this);
    }

    public IDropTarget FirstDropTarget { get; }
    public IDropTarget SecondDropTarget { get; }
}

推荐阅读