如何将UserControl的IsEnabled属性绑定到其Command命令

3

我有一个声明了命令的用户控件:

public static readonly DependencyProperty CommandProperty = 
    DependencyProperty.Register("Command", typeof(ICommand), typeof(MyUserControl));

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

该命令由父UI指定。
如何将UserControl的IsEnabled属性绑定到该命令的可用性?


抱歉,没有仔细阅读... - Marc
你的ICommand是如何实现的?你可以改变或编辑Command的实现吗?如果可以,那么添加一个名为IsEnabled的属性到Command会有所帮助。 - Jehof
2个回答

3

试试这个:

 public static readonly DependencyProperty CommandProperty =
        DependencyProperty.Register("Command", typeof(ICommand), typeof(UserControl1),
                                    new PropertyMetadata(default(ICommand), OnCommandChanged));

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

    private static void OnCommandChanged(DependencyObject dependencyObject,
                                         DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
    {
        var userControl = (UserControl1) dependencyObject;
        var command = dependencyPropertyChangedEventArgs.OldValue as ICommand;
        if (command != null)
            command.CanExecuteChanged -= userControl.CommandOnCanExecuteChanged;
        command = dependencyPropertyChangedEventArgs.NewValue as ICommand;
        if (command != null)
            command.CanExecuteChanged += userControl.CommandOnCanExecuteChanged;
    }

    private void CommandOnCanExecuteChanged(object sender, EventArgs eventArgs)
    {
        IsEnabled = ((ICommand) sender).CanExecute(null);
    }

我尝试了我的例子,一切都正常。你正在使用什么类型的ICommand? - Vyacheslav Volkov
我再次尝试,是的,它可以工作!虽然在这里IsEnabled = ((ICommand) sender).CanExecute(null);会给我一个 sender 作为 null 的消息,所以我直接用 Command 替换了 sender。奇怪的是,这似乎是与 @Viv 的解决方案相同,除了取消事件处理程序的订阅。 - net_prog
Viv的代码很相似,可能你在尝试Viv的解决方案时错过了什么。 - Vyacheslav Volkov
可能与使用的命令类型有关,但是“CommandOnCanExecuteChanged”始终接收到“null”发送器。 直接替换为“Command”成员可以解决此问题。 如果定义了“CommandParameter”,请不要忘记在调用“CanExecute”时包括它。 我最终得到了这个:IsEnabled = Command.CanExecute(CommandParameter); - DonBoitnott

1
你可以尝试这个:

你可以尝试这个:

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

private static void OnCurrentCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
  UserControl1 currentUserControl = d as UserControl1;
  ICommand newCommand = e.NewValue as ICommand;
  if (currentUserControl == null || newCommand == null)
    return;
  newCommand.CanExecuteChanged += (o, args) => currentUserControl.IsEnabled = newCommand.CanExecute(null);
}

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

对我来说运行良好。不确定你的代码结构如何。https://www.dropbox.com/s/91z8t0g8lg33mew/MvvmLight1.rar 是我准备的示例链接。你可以尝试使用它并将其与你拥有的代码进行比较。 - Viv

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接