2012-04-01 5 views
2

This question sagt mir, was in Worten zu tun ist, aber ich kann nicht herausfinden, wie man den Code schreibt. :)MVVM behandelt die Drag-Ereignisse von MouseDragElementBehavior

Ich möchte, dies zu tun:

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

Aber wie die andere Frage skizziert, wird der Eventtrigger nicht funktioniert ... Ich denke, es ist, weil es die DragFinished Veranstaltung auf dem SomeUIElement finden will statt von auf der MouseDragElementBehavior. Ist das korrekt?

Also ich denke, was ich tun möchte, ist:

  • ein Verhalten schreiben, die von MouseDragElementBehavior
  • Aufschalten erbt die OnAttached Methode
  • ... zum DragFinished Ereignis abonnieren, aber ich kann nicht Finde den Code heraus, um dieses Bit zu machen.

Hilfe bitte! :)

Antwort

1

Hier ist, was ich implementiert Ihr Problem zu lösen:

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

Und dann die XAML es so zu nennen ....

 <Interactivity:Interaction.Behaviors> 
      <Controls:MouseDragCustomBehavior Command="{Binding DoCommand}" /> 
     </Interactivity:Interaction.Behaviors> 
Verwandte Themen