昨天在项目中遇到一个问题,按钮bind了Command后,利用CanExecute控制它的是否可点击。结果却在初始化viewmodel的时候执行了一次CanExecute,之后一直不触发,按钮的可用性状态也一直不改变。
 

 public DelegateCommand NewCommand { get; set; }
public DelegateCommand CheckCommand { get; set; }


看了半天,也没看出啥原因,以为是控件的问题,后来又换成普通的Button,结果还是一样,只触发一次。

后来查资料,才知道需要用CommandManager注册下。
修改后代码如下

public class DelegateCommand : ICommand
{
Action _action;
Func<bool> _canAction;
public DelegateCommand(Action action)
{
_action = action;
}
public DelegateCommand(Action action, Func<bool> canAction)
{
_action = action;
_canAction = canAction;
}
public bool CanExecute(object parameter)
{
if (_canAction == null)
{
return true;
}
return _canAction.Invoke();
}
//public event EventHandler CanExecuteChanged;
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
if (_action != null)
{
_action.Invoke();
}
}
}

这样就可以实时触发了。

WPF Command CanExecute 触发一次的问题的更多相关文章

  1. wpf Command canExecute 更新

    可以调用以下语句通知 CommandManager.InvalidateRequerySuggested();

  2. A memory leak issue with WPF Command Binding

    Background In our application, we have a screen which hosts several tabs. In each tab, it contains a ...

  3. silverlight wpf Command提交时输入验证

    silverlight 或WPF在MVVM模式中使用INotifyDataErrorInfo接口对输入进行验证时 控件lostFocus时会触发验证,但在提交动作(例如button的Command)时 ...

  4. WPF 后台数据触发改变界面状态-心跳实现

    今年做的一个上位机工控WPF项目,做个小小的总结把,以后随时来找 请不要带血乱喷,我只是菜鸟.___by 鲍队 类似于这样子的;大致的意思是:一个代码变量,通过改变变量的值,绑定这个变量的这个圆颜色也 ...

  5. WPF Command Binding

    <Window x:Class="WpfTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/200 ...

  6. WPF Command命令模式

    //定义接口 public interface IView { bool IsChanged { get; set; } void SetBinding(); void Clear(); } //定义 ...

  7. WPF之无法触发KeyDown或者KeyUp键盘事件

    有时候我们可能在Panel(StackPanel.Canvas.Grid)上或者是在一些默认不支持Focus的控件上添加了KeyDown或者KeyUp,可是残酷的现实告诉我们,这是无法触发的,怎么办呢 ...

  8. WPF Command

    使用CustomControl时绑定Command用法 C# Part public static RoutedUICommand ClearCommand { get; private set; } ...

  9. 【WPF】代码触发Button点击事件

    先定义Button按钮并绑定事件. public void test() { Button btn = new Button(); btn.Click += Btn_Click; } private ...

随机推荐

  1. NServiceBus SAGA 消息状态驱动

    https://docs.particular.net/tutorials/nservicebus-sagas/1-getting-started/ 链接:https://pan.baidu.com/ ...

  2. oracle 笔记DBA

    1.1oracle开启归档 关闭数据库 SQL>archive log list; SQL>shutdown immediate; SQL>startup mount ; SQL&g ...

  3. socket.io的connect连接时不断的进行自动连接,并产生错误net::ERR_EMPTY_RESPONSE

    socket = io.connect('http://192.168.1.200:9043?uuid=333'); 执行上面的语句时,产生下面的错误: 后来经过排查,是由于项目的jdk版本过低引起的 ...

  4. GOIP connects with Elastix through “config by line”

    GOIP connects with Elastix through “config by line” By grace Liu on May 17, 2013 in Elastix, Gateway ...

  5. 使用adb shell 模拟点击事件

    针对问题[appium无法点击到某一内容,需要使用adb去执行点击事件] 需要命令: adb shell adb devices input [input可以实现的功能]: 输入文本信息:input ...

  6. MySQL中使用SHOW PROFILE命令分析性能的用法整理

    show profile是由Jeremy Cole捐献给MySQL社区版本的.默认的是关闭的,但是会话级别可以开启这个功能.开启它可以让MySQL收集在执行语句的时候所使用的资源.为了统计报表,把pr ...

  7. 扩展方法(深入理解c#)

    1. 静态类到扩展方法: 许多方法可能都适合转为扩展方法,只要具有以下特征: 1)你想为一个类型添加一些成员: 2)你不需要为类型的实例添加更多的数据: 3)你不能改变类型本身,因为是别人的代码 2. ...

  8. yii2自定义json格式success,error跳转

    /** * ---------------------------------------------- * 操作成功跳转的快捷方法 * @access protected * @param stri ...

  9. mysql 查询中文字段 没有结果的解决方法

    代码如下: $conn = new mysqli('localhost', 'root', '', 'excel');$sql = "select 中信一级行业 from excel gro ...

  10. mysql学习之路_sql

    查看数据库: Show databases; 查看指定部分数据库:模糊查询 Show databases like ‘patten’;--paatten是匹配模式 %:表示是匹配模式 _:表示匹配单个 ...