使用Caliburn Micro Framework将文件拖放到WPF中 [英] Drag and Drop Files into WPF with Caliburn Micro Framework

查看:50
本文介绍了使用Caliburn Micro Framework将文件拖放到WPF中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Caliburn Micro框架编写的WPF应用程序,用于MVVM。

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

我发现了一些示例,这些示例接受从usercontrol的代码隐藏文件拖入wpf应用程序的文件。我还没有找到使用MVVM方法正确执行此操作的示例?

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?

推荐答案

我拼凑了一些东西,这些东西可以解决我现在和现在的需求。可能更笼统。

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; }
}

这篇关于使用Caliburn Micro Framework将文件拖放到WPF中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆