2009-06-27 5 views
0

Ich verwende die Julmar Hilfsklassen für WPF, so dass ich eine benutzerdefinierte ICommand auf ein Ereignis wie MouseEnter- auf einem Textfeld wie so nennen kann:Julmar WPF Helfer Parameter

<TextBox Text="hmm"> 
    <julmar:EventCommander.Mappings>  
     <julmar:CommandEvent Command="{Binding DataContext.IncreaseQueueTimeCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}" Event="MouseEnter" /> 
    </julmar:EventCommander.Mappings> 
</TextBox> 

Dies funktioniert und ruft den Befehl Das Problem ist, dass ich ein Objekt als Parameter übergeben muss. Weiß jemand, ob das möglich ist? Die Dokumentation scheint ziemlich leicht zu sein.

Früher war ich in der Lage, das Objekt als Parameter wie folgt weitergeben müssen:

<Button Content="Save" x:Name="SaveQueueTimeButton" Command="{Binding DataContext.SaveQueueTimeCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}" CommandParameter="{Binding}" /> 

Aber offensichtlich ist dies nicht das, was ich brauche, wie es auf einem Mouse

Jede Hilfe wird nicht ausgelöst würde nützlich sein,

Dank

Antwort

1

Sie dies mit dem Verhaltensmuster lösen können. Grundsätzlich erstellen Sie eine benutzerdefinierte Klasse mit zwei Abhängigkeitseigenschaften: Command und CommandParameter. Sie registrieren auch einen Handler für beide Eigenschaften.

In einem der Handler, erhalten Sie Ihre TextBox als Parameter übergeben. Jetzt können Sie sich mit den Ereignissen verbinden, an denen Sie interessiert sind. Wenn nun einer der registrierten Event-Handler aufgerufen wird, können Sie den gebundenen Befehl mit dem gebundenen Befehlsparameter aufrufen.

Hier ist ein Codebeispiel:

public class CommandHelper 
{ 
    // 
    // Attached Properties 
    // 
    public static ICommand GetCommand(DependencyObject obj) 
    { 
     return (ICommand)obj.GetValue(CommandProperty); 
    } 

    public static void SetCommand(DependencyObject obj, ICommand value) 
    { 
     obj.SetValue(CommandProperty, value); 
    } 

    public static readonly DependencyProperty CommandProperty = 
     DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(CommandHelper), new UIPropertyMetadata(null)); 



    public static object GetCommandParameter(DependencyObject obj) 
    { 
     return (object)obj.GetValue(CommandParameterProperty); 
    } 

    public static void SetCommandParameter(DependencyObject obj, object value) 
    { 
     obj.SetValue(CommandParameterProperty, value); 
    } 

    public static readonly DependencyProperty CommandParameterProperty = 
     DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(CommandHelper), new UIPropertyMetadata(null)); 


    // 
    // This property is basically only there to attach handlers to the control that will be the command source 
    // 
    public static bool GetIsCommandSource(DependencyObject obj) 
    { 
     return (bool)obj.GetValue(IsCommandSourceProperty); 
    } 

    public static void SetIsCommandSource(DependencyObject obj, bool value) 
    { 
     obj.SetValue(IsCommandSourceProperty, value); 
    } 

    public static readonly DependencyProperty IsCommandSourceProperty = 
     DependencyProperty.RegisterAttached("IsCommandSource", typeof(bool), typeof(CommandHelper), new UIPropertyMetadata(false, OnRegisterHandler)); 


    // 
    // Here you can register handlers for the events, where you want to invoke your command 
    // 
    private static void OnRegisterHandler(DependencyObject obj, DependencyPropertyChangedEventArgs e) 
    { 
     FrameworkElement source = obj as FrameworkElement; 

     source.MouseEnter += OnMouseEnter; 
    } 

    private static void OnMouseEnter(object sender, MouseEventArgs e) 
    { 
     DependencyObject source = sender as DependencyObject; 
     ICommand command = GetCommand(source); 
     object commandParameter = GetCommandParameter(source); 

     // Invoke the command 
     if (command.CanExecute(commandParameter)) 
      command.Execute(commandParameter); 
    } 
} 

Und XAML:

<TextBox Text="My Command Source" 
      local:CommandHelper.IsCommandSource="true" 
      local:CommandHelper.Command="{Binding MyCommand}" 
      local:CommandHelper.CommandParameter="MyParameter" /> 
+0

Könnten Sie bitte eine Probe zur Verfügung stellen? Mein Gehirn endete in einem riesigen Knoten nach dem Lesen Ihrer Beschreibung. – chrischu

+0

Sie können das obige Codebeispiel jetzt finden. – Andrej

0

Wenn die Julmar CommandEvent keine Command Eigenschaft haben, empfehle ich Ihnen Marlon Grech Attached Command Behaviours stattdessen verwenden. Es ist sehr ähnlich, aber es bietet eine CommandParameter-Eigenschaft.