Xaml里如下:
<ComboBox x:Name="comboType" Grid.Column="1" Grid.Row="4" HorizontalAlignment="Left" VerticalAlignment="Center" Width="150" Text="{Binding Question.Type}"> <i:Interaction.Triggers> <i:EventTrigger EventName="SelectionChanged"> <i:InvokeCommandAction Command="{Binding SetTypeCommand}" CommandParameter="{Binding SelectedIndex,ElementName=comboType}" /> </i:EventTrigger> </i:Interaction.Triggers> <ComboBoxItem Content="设置题型..."></ComboBoxItem> <ComboBoxItem Content="单选题"></ComboBoxItem> <ComboBoxItem Content="多选题"></ComboBoxItem> </ComboBox>
viewModel里如下:
private DelegateCommand<int> _setTypeCommand; public DelegateCommand<int> SetTypeCommand { get { return _setTypeCommand??(_setTypeCommand = new DelegateCommand<int>(SetType)); } set { _setTypeCommand = value; } } private void SetType(int selectedIndex) { // }
运行时就提示错误:T for DelegateCommand<T> is not an object nor Nullable。
试了下,如果这个T为string的话就不会出错,但为 int的话就报错,请问为什么?该怎么解决?谢谢!
改用
int?
引用类型和值类型的区别
我也遇到这个问题了,你解决了吗?
定义用int?
private DelegateCommand<int?> _setTypeCommand;