刷新WPF命令 [英] Refresh WPF Command

查看:105
本文介绍了刷新WPF命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道我如何强制CanExecute在自定义命令上被调用(乔什·史密斯的 RelayCommand )?

Does anyone know how I can force CanExecute to get called on a custom command (Josh Smith's RelayCommand)?

通常,只要在UI上发生交互,就会调用CanExecute.如果单击某些内容,则命令将更新.

Typically, CanExecute is called whenever interaction occurs on the UI. If I click something, my commands are updated.

我遇到一种情况,其中CanExecute的条件被幕后的计时器打开/关闭.因为这不是由用户交互驱动的,所以在用户与UI交互之前不会调用CanExecute.最终结果是我的Button保持启用/禁用状态,直到用户单击它为止.单击后,它会正确更新.有时Button似乎已启用,但是当用户单击它时,它变为禁用而不是触发.

I have a situation where the condition for CanExecute is getting turned on/off by a timer behind the scenes. Because this is not driven by user interaction, CanExecute is not called until the user interacts with the UI. The end result is that my Button remains enabled/disabled until the user clicks on it. After the click, it is updated correctly. Sometimes the Button appears enabled, but when the user clicks it changes to disabled instead of firing.

当计时器更改影响CanExecute的属性时,如何强制代码更新?我尝试在影响CanExecute的属性上触发PropertyChanged(INotifyPropertyChanged),但这无济于事.

How can I force an update in code when the timer changes the property that affects CanExecute? I tried firing PropertyChanged (INotifyPropertyChanged) on the property that affects CanExecute, but that did not help.

示例XAML:

<Button Content="Button" Command="{Binding Cmd}"/>

后面的示例代码:

private ICommand m_cmd;
public ICommand Cmd
{
    if (m_cmd == null)
        m_cmd = new RelayCommand(
            (param) => Process(),
            (param) => EnableButton);

    return m_cmd;
}

// Gets updated from a timer (not direct user interaction)
public bool EnableButton { get; set; }

推荐答案

CommandManager.InvalidateRequerySuggested()

这篇关于刷新WPF命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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