Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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
C# 将命令属性添加到任何自定义控件_C#_Wpf_Mvvm_Icommand - Fatal编程技术网

C# 将命令属性添加到任何自定义控件

C# 将命令属性添加到任何自定义控件,c#,wpf,mvvm,icommand,C#,Wpf,Mvvm,Icommand,我可以用下面的代码导航到另一个视图。我想使用tabControl和自定义控件进行导航,但没有用于绑定myCommand的命令属性。 那么,如何将命令绑定到自定义控件 <Button Command="{Binding myCommand}" Content="Nav"/> 1.还有什么需要绑定的吗? 2.如何将命令属性添加到自定义控件?CustomControls和UserControls 为命令创建DependencyProperty public class MyControl

我可以用下面的代码导航到另一个视图。我想使用tabControl和自定义控件进行导航,但没有用于绑定myCommand的命令属性。 那么,如何将命令绑定到自定义控件

<Button Command="{Binding myCommand}" Content="Nav"/>
1.还有什么需要绑定的吗? 2.如何将命令属性添加到自定义控件?

CustomControls和UserControls 为命令创建DependencyProperty

public class MyControl : Control
{
    static MyControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl), new FrameworkPropertyMetadata(typeof(MyControl)));
    }

    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }

    public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
        "Command",
        typeof(ICommand),
        typeof(MyControl),
        new PropertyMetadata(null, OnCommandPropertyChanged));

    private static void OnCommandPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        MyControl control = d as MyControl;
        if (control == null) return;

        control.MouseLeftButtonDown -= OnControlLeftClick;
        control.MouseLeftButtonDown += OnControlLeftClick;
    }

    private static void OnControlLeftClick(object sender, MouseButtonEventArgs e)
    {
        MyControl control = sender as MyControl;
        if (control == null || control.Command == null) return;

        ICommand command = control.Command;

        if (command.CanExecute(null))
            command.Execute(null);
    }
}
xaml:

xaml:

您可以将事件更改为MouseLeftButtonDown以外的内容,如果需要,可以为CommandParameter添加DependencyProperty AttachedProperty。

CustomControls和UserControls 为命令创建DependencyProperty

public class MyControl : Control
{
    static MyControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl), new FrameworkPropertyMetadata(typeof(MyControl)));
    }

    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }

    public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
        "Command",
        typeof(ICommand),
        typeof(MyControl),
        new PropertyMetadata(null, OnCommandPropertyChanged));

    private static void OnCommandPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        MyControl control = d as MyControl;
        if (control == null) return;

        control.MouseLeftButtonDown -= OnControlLeftClick;
        control.MouseLeftButtonDown += OnControlLeftClick;
    }

    private static void OnControlLeftClick(object sender, MouseButtonEventArgs e)
    {
        MyControl control = sender as MyControl;
        if (control == null || control.Command == null) return;

        ICommand command = control.Command;

        if (command.CanExecute(null))
            command.Execute(null);
    }
}
xaml:

xaml:


您可以将事件更改为MouseLeftButtonDown以外的内容,如果需要,可以为CommandParameter添加DependencyProperty AttachedProperty。

每个UIElement都有所谓的

输入绑定支持将命令绑定到输入设备。例如,MouseBinding实现输入绑定,其中包含鼠标设备特有的属性


因此,不要注册新命令和命令参数DP并处理事件来触发命令,而是尝试使用现成的功能,每个UIElement都有所谓的

输入绑定支持将命令绑定到输入设备。例如,MouseBinding实现输入绑定,其中包含鼠标设备特有的属性


因此,与其注册新命令和命令参数DP并处理事件以触发命令,不如尝试使用现成的功能

@Ash:我熟悉输入绑定,我同意完整的答案应该包括这一点。撇开添加CommandProperty的问题不谈,我发现在我的自定义控件上有CommandProperty更方便,尽管我们可以在按钮上不使用InputBindings。还考虑到除了键盘/鼠标输入之外,还有可能执行命令。@ AH:我熟悉输入绑定,我同意一个完整的答案应该包括这个。撇开添加CommandProperty的问题不谈,我发现在我的自定义控件上有CommandProperty更方便,尽管我们可以在按钮上不使用InputBindings。还考虑到除了键盘/鼠标输入之外,有可能执行命令。
public static class ExecutesCommandOnLeftClickBehavior
{
    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(ExecutesCommandOnLeftClickBehavior),
        new PropertyMetadata(null, OnCommandPropertyChanged));

    private static void OnCommandPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        UIElement element = d as UIElement;
        if (element == null) return;

        element.MouseLeftButtonDown -= OnMouseLeftButtonDown;
        element.MouseLeftButtonDown += OnMouseLeftButtonDown;
    }

    private static void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        UIElement element = sender as UIElement;
        if (element == null) return;

        ICommand command = GetCommand(element);
        if (command == null) return;

        if (command.CanExecute(null))
            command.Execute(null);
    }
}
<Grid local:ExecutesCommandOnLeftClickBehavior.Command="{Binding SomeCommand}"/>
<Label Content="new">
    <Label.InputBindings>
        <MouseBinding Gesture="LeftClick" 
                      Command="{Binding Path=myCommand}" 
                      CommandParameter="smth"/>
    </Label.InputBindings>
</Label>