2012-04-01 27 views
2

This questionは、単語で何をすべきかを教えてくれますが、コードを書く方法を理解できません。 :)私はこれやりたいMouseDragElementBehaviorのDragイベントを処理するMVVM

<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は動作しませんが...私はそれが代わりにSomeUIElementDragFinishedイベントを見つけたいので、それはだと思いますMouseDragElementBehaviorのあれは正しいですか?

  • MouseDragElementBehavior
  • オーバーライド
  • が... DragFinishedイベントに購読OnAttached方法を継承する行動を書くが、私がすることはできません:

    は、だから私は私がやりたいことだと思いますこのビットを行うコードを見つけてください。

お願いします! :)ここで

答えて

1

は、私はあなたの問題を解決するために実装するものである:

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は

 <Interactivity:Interaction.Behaviors> 
      <Controls:MouseDragCustomBehavior Command="{Binding DoCommand}" /> 
     </Interactivity:Interaction.Behaviors> 
....このようにそれを呼び出すために