[英]Drag and Drop Files into WPF with Caliburn Micro Framework
I have a WPF app written using the Caliburn Micro framework for MVVM stuff. 我有一个使用Caliburn Micro框架编写的WPF应用程序,用于MVVM。
I have found examples of accepting a dragged file into the wpf app from the codebehind file of the usercontrol. 我已经找到了接受从usercontrol的代码隐藏文件拖入wpf应用程序的文件的示例。 I have not been able to find an example of how to proper do this using a MVVM approach ?
我无法找到使用MVVM方法如何正确执行此操作的示例?
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; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.