按钮命令CanExecute不叫物业时改变 [英] Button Command CanExecute not called when property changed

查看:196
本文介绍了按钮命令CanExecute不叫物业时改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框和一个按钮的形式。

I have a form with a textbox and a button.

在该文本框有它的价值变化,按钮命令不把它的命令的CanExecute方法

When that textbox has it's value changed, the button command doesn't call the CanExecute method of it's command.

命令参数设置,但似乎并没有改变。
负荷窗口后,按钮保持禁用

The command parameter is set but doesn't seem to change. After load the window, the button remains disabled.

<TextBox Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<Button Content="Save" Command="{Binding SaveChangesCommand}" CommandParameter="{Binding Name}" />



我知道的工作,因为我创造了一个接收目标绑定行为,并提高CanExecute时绑定在绑定的变化。

I know the binding is working because I created a behavior that receives a target binding and raise the CanExecute when the the binding changes.

通过这种行为,在CanExecute通常叫做

With this behavior, the CanExecute is called normally.

<Button Content="Save" Command="{Binding SaveChangesCommand}">
    <i:Interaction.Behaviors>
        <behaviors:CallCommandCanExecuteWhenBindingChange Target="{Binding Name}" />
    </i:Interaction.Behaviors>
</Button>



视图模型:

ViewModel:

public class EditViewModel : INotifyPropertyChanged
{
    private string _name;

    public EditViewModel()
    {
        SaveChangesCommand = new DelegateCommand(p => SaveChanges(), p => CanSaveChanges());
    }

    public string Name
    {
        get { return _name; }
        set
        {
            if (value == _name) return;
            _name = value;
            OnPropertyChanged();
        }
    }

    public DelegateCommand SaveChangesCommand { get; private set; }

    private void SaveChanges()
    {
    }
    private bool CanSaveChanges()
    {
        return !string.IsNullOrWhiteSpace(Name);
    }
}



DelegateCommand:

DelegateCommand:

public interface IBaseCommand : ICommand
{
    void OnCanExecuteChanged();
}

public class DelegateCommand : IBaseCommand
{
    private readonly Action<object> _execute;
    private readonly Func<object, bool> _canExecute;

    public DelegateCommand(Action<object> execute, Func<object, bool> canExecute)
    {
        _execute = execute;
        _canExecute = canExecute;
    }

    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object parameter)
    {
        return _canExecute(parameter);
    }
    public void Execute(object parameter)
    {
        _execute(parameter);
        OnCanExecuteChanged();
    }

    public void OnCanExecuteChanged()
    {
        var handler = CanExecuteChanged;
        if (handler != null)
            handler(this, EventArgs.Empty);
    }
}



CallCommandCanExecuteWhenBindingChange:

CallCommandCanExecuteWhenBindingChange:

public class CallCommandCanExecuteWhenBindingChange : Behavior<FrameworkElement>
{
    public static readonly DependencyProperty<CallCommandCanExecuteWhenBindingChange, object> TargetProperty;
    private ICommandBase _command;

    static CallCommandCanExecuteWhenBindingChange()
    {
        var dependency = new DependencyRegistry<CallCommandCanExecuteWhenBindingChange>();

        TargetProperty = dependency.Register(b => b.Target, s => s.OnTargetChange());
    }

    public object Target
    {
        get { return TargetProperty.Get(this); }
        set { TargetProperty.Set(this, value); }
    }

    private void OnTargetChange()
    {
        if (_command == null && AssociatedObject != null)
        {
            var field = AssociatedObject.GetType().GetProperty("Command");
            _command = (IBaseCommand)field.GetValue(AssociatedObject);
        }

        if (_command != null)
            _command.OnCanExecuteChanged();
    }
}



有谁知道为什么按钮不叫CanExecute?

Does anyone know why the button doesn't call the CanExecute?

推荐答案

在你的 DelegateCommand 实施 CanExecuteChanged 要添加/删除的 CommandManager.RequerySuggested 事件

In your DelegateCommand implementation CanExecuteChanged should add/remove to CommandManager.RequerySuggested event

时发生的命令管理检测到可能会改变一个命令来执行的能力条件。

Occurs when the CommandManager detects conditions that might change the ability of a command to execute.

将其更改为

public event EventHandler CanExecuteChanged
{
    add { CommandManager.RequerySuggested += value; }
    remove { CommandManager.RequerySuggested -= value; }
}

这篇关于按钮命令CanExecute不叫物业时改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆