MVVM处理MouseDragElementBehavior的拖放事件 [英] MVVM handling the Drag events of MouseDragElementBehavior
本文介绍了MVVM处理MouseDragElementBehavior的拖放事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这个问题告诉我该怎么用言语做,但我无法弄清楚如何写代码。 :)
This question tells me what to do in words, but I can't figure out how to write the code. :)
我想这样做:
<SomeUIElement>
<i:Interaction.Behaviors>
<ei:MouseDragElementBehavior ConstrainToParentBounds="True">
<i:Interaction.Triggers>
<i:EventTrigger EventName="DragFinished">
<i:InvokeCommandAction Command="{Binding SomeCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ei:MouseDragElementBehavior>
</i:Interaction.Behaviors>
</SomeUIElement>
但是,随着其他问题概括的的EventTrigger不能正常工作......我认为这是因为它想要找上了 MouseDragElementBehavior $ C上,而不是
SomeUIElement
的 DragFinished
事件$ C>。 ?那是正确的。
But as the other question outlines, the EventTrigger doesn't work... I think it's because it wants to find the DragFinished
event on the SomeUIElement
instead of on the MouseDragElementBehavior
. Is that correct?
所以我觉得我想要做的是:
So I think what I want to do is:
- 编写从
MouseDragElementBehavior
行为 - 重写
OnAttached
方法 - 订阅
DragFinished
事件......但我想不通的代码要做到这一点位。
- Write a behavior that inherits from
MouseDragElementBehavior
- Override the
OnAttached
method - Subscribe to the
DragFinished
event... but I can't figure out the code to do this bit.
请帮助! :)
推荐答案
下面是我为了解决你的问题:
Here is what I implemented to solve your problem :
public class MouseDragCustomBehavior : MouseDragElementBehavior
{
public static DependencyProperty CommandProperty =
DependencyProperty.Register("Command", typeof(ICommand), typeof(MouseDragCustomBehavior));
public static DependencyProperty CommandParameterProperty =
DependencyProperty.Register("CommandParameter", typeof(object), typeof(MouseDragCustomBehavior));
protected override void OnAttached()
{
base.OnAttached();
if (!DesignerProperties.GetIsInDesignMode(this))
{
base.DragFinished += MouseDragCustomBehavior_DragFinished;
}
}
private void MouseDragCustomBehavior_DragFinished(object sender, MouseEventArgs e)
{
var command = this.Command;
var param = this.CommandParameter;
if (command != null && command.CanExecute(param))
{
command.Execute(param);
}
}
protected override void OnDetaching()
{
base.OnDetaching();
base.DragFinished -= MouseDragCustomBehavior_DragFinished;
}
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
public object CommandParameter
{
get { return GetValue(CommandParameterProperty); }
set { SetValue(CommandParameterProperty, value); }
}
}
和则XAML来称呼它。 ...
And then the XAML to call it like this....
<Interactivity:Interaction.Behaviors>
<Controls:MouseDragCustomBehavior Command="{Binding DoCommand}" />
</Interactivity:Interaction.Behaviors>
这篇关于MVVM处理MouseDragElementBehavior的拖放事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文