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

问题描述 投票:0回答:2

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

<Button Command="{Binding myCommand}" Content="Nav"/>

无论如何要绑定? 2.如何将命令属性添加到自定义控件?

c# wpf mvvm icommand
2个回答
0
投票

CustomControls and 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:

<local:MyControl Command="{Binding SomeCommand}"/>

Other controls

创建附加行为。

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

XAML:

<Grid local:ExecutesCommandOnLeftClickBehavior.Command="{Binding SomeCommand}"/>

您可以将事件更改为MouseLeftButtonDown以外的其他内容,如果需要,可以为CommandParameter添加DependencyPropertyAttachedProperty)。


2
投票

每个UIElement都有所谓的InputBindings

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

<Label Content="new">
    <Label.InputBindings>
        <MouseBinding Gesture="LeftClick" 
                      Command="{Binding Path=myCommand}" 
                      CommandParameter="smth"/>
    </Label.InputBindings>
</Label>

因此,不是注册新的Command和CommandParameter DP并处理触发Command的事件,而是尝试使用开箱即用的功能

© www.soinside.com 2019 - 2024. All rights reserved.