ICommand doesn't update the IsEnabled on a button using CanExecute

与世无争的帅哥 提交于 2019-12-10 17:18:22

问题


I have a very simple button binded to a command

    <Button Content="Add" Margin="10,10,10,0" Command="{Binding SaveCommand}" ></Button>

My command code

    public ICommand SaveCommand
    {
        get;
        internal set;
    }

    private bool CanExecuteSaveCommand()
    {
        return DateTime.Now.Second % 2 == 0;
    }

    private void CreateSaveCommand()
    {
        SaveCommand = new DelegateCommand(param => this.SaveExecute(), param => CanExecuteSaveCommand());
    }

    public void SaveExecute()
    {
        PharmacyItem newItem = new PharmacyItem();
        newItem.Name = ItemToAdd.Name;
        newItem.IsleNumber = ItemToAdd.IsleNumber;
        newItem.ExpDate = ItemToAdd.ExpDate;
        PI.Add(newItem);
    }

The code effectively blocks the command from running based on CanExecuteSaveCommand but the button is never disabled, is there a way to achieve this?


回答1:


ICommand.CanExecute() is called automatically by WPF whenever it thinks the command availability may have changed. This generally tends to be on user activity, eg keyboard events, focus events etc.

Since your command availability changes purely based on time, WPF has no way of guessing that it has changed. Instead you need to give it a hint by calling CommandManager.InvalidateRequerySuggested();

Since your command availability changes every second, you would need to set up a timer to call this function at least every second.

Note that although InvalidateRequerySuggested() is the easiest solution, it will cause WPF to re-evaluate ALL command availabilities. If this is a performance problem, you can raise the CanExecuteChanged event on your ICommand instance instead.



来源:https://stackoverflow.com/questions/23555590/icommand-doesnt-update-the-isenabled-on-a-button-using-canexecute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!