簡體   English   中英

屬性更改時,不會調用按鈕命令CanExecute

[英]Button Command CanExecute not called when property changed

我有一個帶有文本框和按鈕的表單。

當該文本框的值發生變化時,button命令不會調用它的命令的CanExecute方法。

命令參數已設置但似乎沒有更改。 加載窗口后,該按鈕保持禁用狀態。

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

我知道綁定工作正常,因為我創建了一個接收目標綁定的行為,並在綁定更改時引發CanExecute。

通過此行為,可以正常調用CanExecute。

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

視圖模型:

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:

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:

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?

在您的DelegateCommand實現中, CanExecuteChanged應該添加/刪除CommandManager.RequerySuggested事件

在CommandManager檢測到可能會更改命令執行能力的條件時發生。

改為

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM