记录一次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);
posted @ 2024-03-06 12:25  孤沉  阅读(168)  评论(0编辑  收藏  举报