记录一次WPF命令参数报错,InvalidCastException: T for DelegateCommand<T> is not an object nor Nullable.

在使用WPF的时候对int或者bool类型进行绑定出现InvalidCastException: T for DelegateCommand is not an object nor Nullable.

 <Button Width="200" Height="30" Content="按钮" Command="{Binding OpenCommand}" CommandParameter="{Binding Msg}"/>
 
  OpenCommand = new DelegateCommand<int/bool>(ExecuteOpen);

解决方案如下:
1、加上非空? ,以下是int类型写法

 OpenCommand = new DelegateCommand<int?>(ExecuteOpen);

2、使用Nullable+泛型,下面是bool类型写法

 OpenCommand = new DelegateCommand<Nullable<bool>>(ExecuteOpen);

本文作者:孤沉

本文链接:https://www.cnblogs.com/guchen33/p/18056271

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   孤沉  阅读(214)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
展开