Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Julmar WPF辅助参数_Wpf_Command - Fatal编程技术网

Julmar WPF辅助参数

Julmar WPF辅助参数,wpf,command,Wpf,Command,我正在为WPF使用Julmar helper类,这样我就可以在事件(如文本框上的MouseEnter)上调用自定义ICommand,如下所示: <TextBox Text="hmm"> <julmar:EventCommander.Mappings> <julmar:CommandEvent Command="{Binding DataContext.IncreaseQueueTimeCommand, RelativeSource={

我正在为WPF使用Julmar helper类,这样我就可以在事件(如文本框上的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}" />

但很明显,这不是我需要的,因为它不会在鼠标上开火

任何帮助都是有用的


谢谢

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

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

下面是一个代码示例:

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


如果Julmar CommandEvent没有CommandParameter属性,我建议您使用Marlon Grech属性。它非常相似,但它提供了CommandParameter属性。

您能提供一个示例吗?读了你的描述后,我的大脑陷入了一个巨大的纠结。