记录一次WPF命令参数报错,InvalidCastException: T for DelegateCommand<T> is not an object nor Nullable.
在使用WPF的时候对int或者bool类型进行绑定出现InvalidCastException: T for DelegateCommand
<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);