c# - 使用Caliburn Micro Framework将文件拖放到WPF中

标签 c# wpf mvvm caliburn.micro

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

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

有关如何执行此操作的任何提示?

最佳答案

我一起摸索解决了现在和现在的需求。可能更一般。

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

关于c# - 使用Caliburn Micro Framework将文件拖放到WPF中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21078838/

相关文章:

c# - AppDomain 中 Enum 的奇怪 SerializationException

c# - ListView ComputedVerticalScrollBarVisibilityProperty 总是返回 Visible?

c# - 为什么在 DataGridTextcolumn 中找不到 ObservableCollection 中实际类的属性,但父类属性是?

c# - 在 ViewModel 中更新 View

c# - WPF/MVVM - 检查基于字符串匹配的 MenuItem

c# - 填充 ComboBox 的值和文本

c# - 如何使用 C# 在网页的源代码中查找 div 中的文本

c# - 在 itextsharp 中 Pades LTV 验证抛出 The Uri Prefix is not Recognized

c# - 如何绘制格式化文本(如果不在 onRender 方法中)

javascript - WPF WebBrowser 甚至在使用 ObjectForScripting 的单独线程上锁定 UI