Julmar WPF助手参数

时间:2009-06-27 10:12:41

标签: wpf command

我正在使用WPF的Julmar帮助程序类,以便我可以在文本框上的MouseEnter事件上调用自定义ICommand,如下所示:

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

这工作并调用命令,问题是我需要传递一个对象作为参数,有人知道这是否可能?文档似乎很轻松。

以前我能够将对象作为参数传递:

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

但显然这不是我需要的,因为它不会触发MouseEvent

任何帮助都会有用,

由于

2 个答案:

答案 0 :(得分:1)

您可以使用行为模式解决此问题。基本上,您使用两个依赖项属性创建自定义类:Command和CommandParameter。您还为两个属性注册了一个处理程序。

在其中一个处理程序中,您将TextBox作为参数传递。现在您可以连接到您感兴趣的事件。如果现在调用了一个已注册的事件处理程序,您可以使用bound命令参数调用bound命令。

以下是代码示例:

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

和Xaml:

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

答案 1 :(得分:0)

如果Julmar CommandEvent没有CommandParameter属性,我建议您改用Marlon Grech的Attached Command Behaviours。它非常相似,但它提供了一个CommandParameter属性。