2009-06-27 10 views
0

J'utilise les classes d'aide Julmar pour WPF afin que je puisse appeler une coutume ICommand sur un événement tel que MouseEnter sur une zone de texte comme ceci:paramètres d'aide Julmar WPF

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

Cela fonctionne et appelle la commande , le problème est que je dois passer un objet en paramètre, est-ce que quelqu'un sait si c'est possible? la documentation semble assez légère.

Auparavant j'ai pu passer l'objet en tant que paramètre comme ceci:

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

Mais évidemment, ce n'est pas ce que je dois, car il ne se déclenche pas sur mouseEvent

Toute aide être utile,

Merci

Répondre

1

Vous pouvez résoudre cela avec le comportement modèle. Fondamentalement, vous créez une classe personnalisée avec deux propriétés de dépendance: Command et CommandParameter. Vous enregistrez également un gestionnaire pour les deux propriétés.

Dans l'un des gestionnaires, vous obtenez votre TextBox passé en tant que paramètre. Vous pouvez maintenant vous connecter aux événements qui vous intéressent. Si l'un des gestionnaires d'événements enregistrés est appelé, vous pouvez appeler la commande bound avec le paramètre de commande bound.

Voici un exemple de code:

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

et XAML:

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

Pourriez-vous nous fournir un échantillon s'il vous plaît? Mon cerveau s'est retrouvé dans un énorme nœud après avoir lu votre description. – chrischu

+0

Vous pouvez maintenant trouver l'exemple de code ci-dessus. – Andrej

0

Si le Julmar CommandEvent ne dispose pas d'une propriété CommandParameter, je vous suggère d'utiliser Marlon Grech de Attached Command Behaviours à la place. C'est très similaire mais il fournit une propriété CommandParameter.

Questions connexes