Drag and Drop Files into WPF with Caliburn Micro Framework

做~自己de王妃 提交于 2019-12-11 13:53:43

问题


I have a WPF app written using the Caliburn Micro framework for MVVM stuff.

I have found examples of accepting a dragged file into the wpf app from the codebehind file of the usercontrol. I have not been able to find an example of how to proper do this using a MVVM approach ?

Any hints on how to do this?


回答1:


I haxed something together that solved my needs here and now. Could be more general.

            <i:Interaction.Triggers>
                <Trigger:RoutedEventTrigger EventName="DragQuery">
                    <cal:ActionMessage MethodName="DragQuery">
                        <cal:Parameter Value="$source" />
                        <cal:Parameter Value="$eventArgs" />
                        <cal:Parameter Value="$view" />
                    </cal:ActionMessage>
                </Trigger:RoutedEventTrigger>
                <Trigger:RoutedEventTrigger EventName="DropQuery">
                    <cal:ActionMessage MethodName="DropQuery">
                        <cal:Parameter Value="$source" />
                        <cal:Parameter Value="$eventArgs" />
                        <cal:Parameter Value="$view" />
                    </cal:ActionMessage>
                </Trigger:RoutedEventTrigger>

            </i:Interaction.Triggers>

public class RoutedEventTrigger : EventTriggerBase<DependencyObject>
{
    RoutedEvent _routedEvent;
    public RoutedEvent RoutedEvent
    {
        get { return _routedEvent; }
        set { _routedEvent = value; }
    }
    public string EventName { get; set; }

    public RoutedEventTrigger() { }
    protected override void OnAttached()
    {
        switch (EventName)
        {
            case "DragQuery":
                RoutedEvent = UIElement.DragEnterEvent;
                break;
            case "DropQuery":
                RoutedEvent = UIElement.DropEvent;
                break;
            default:
                break;
        }

        Behavior behavior = base.AssociatedObject as Behavior;
        FrameworkElement associatedElement = base.AssociatedObject as FrameworkElement;
        if (behavior != null)
        {
            associatedElement = ((IAttachedObject)behavior).AssociatedObject as FrameworkElement;
        }
        if (associatedElement == null)
        {
            throw new ArgumentException("Routed Event trigger can only be associated to framework elements");
        }
        if (RoutedEvent != null)
        { associatedElement.AddHandler(RoutedEvent, new RoutedEventHandler(this.OnRoutedEvent)); }
    }
    void OnRoutedEvent(object sender, RoutedEventArgs args)
    {
        base.OnEvent(args);
    }
    protected override string GetEventName() { return RoutedEvent.Name; }
}


来源:https://stackoverflow.com/questions/21078838/drag-and-drop-files-into-wpf-with-caliburn-micro-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!