昨天在项目中遇到一个问题,按钮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. c++11 并发 条件变量 超时等待的代码练习

    资料地址 http://en.cppreference.com/w/cpp/thread/condition_variable/wait_until http://www.cnblogs.com/ha ...

  2. tomcat+bean例子

    C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\app\WEB-INF\classes\文件夹下 建立beanTestPa ...

  3. 利用PHPExcel读取excel文件

    $filePath = "7788.xls"; $PHPExcel = new PHPExcel(); $PHPReader = new PHPExcel_Reader_Excel ...

  4. Hibernate学习笔记:基础模型类

    根据业务建模型时,有一些字段基本每个表都是需要的,如下表: package com.company.jelly.model; import javax.persistence.EntityListen ...

  5. Microsoft translater

    professional  tranlater tool : https://translator.microsoft.com/neural/

  6. “matplotlib display text must have all code points < 128 or use Unicode strings”解决方法

    import sys reload(sys) sys.setdefaultencoding('utf-8') 插入以上代码,便可解决.

  7. 数组方法indexOf & lastIndexOf

    indexOf() 语法:arrayObject.indexOf(searchvalue, startIndex) 功能:从数组的开头(位置0)开始向后查找. 参数:searchvalue:必需,要查 ...

  8. 2019.01.22 SCU4444 Travel(最短路+bfs)

    传送门 题意简述:给出一张nnn个点的完全图,有mmm条边边权为aaa其余点边权为bbb,问从111到nnn的最短路. 思路:分类讨论一波即可. (1,n)(1,n)(1,n)的边权为aaa,那么只用 ...

  9. 2019.01.19 bzoj3653: 谈笑风生(长链剖分优化dp)

    传送门 长链剖分优化dpdpdp水题. 题意简述:给一棵树,mmm次询问,每次给一个点aaa和一个值kkk,询问满足如下条件的三元组(a,b,c)(a,b,c)(a,b,c)的个数. a,b是c的祖先 ...

  10. [转]ajQuery的deferred对象详解

    来自:http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_object.html 作者:  ...