2012-04-01 2 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 이벤트를 찾을 싶어 있기 때문에 생각 ... 작동하지 않습니다

of the the 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> 
관련 문제