【发布时间】:2018-03-27 23:48:07
【问题描述】:
我有一个问题,我在 WPF 中有一个如下所示的组合框:
<ComboBox IsSynchronizedWithCurrentItem="True"
commands:PropertyChangeBehavior.Command="{Binding GetCurrentsModuleCommand}"
SelectedItem="{Binding SelectedModule, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
<ComboBox.ItemsSource>
<CompositeCollection>
<ComboBoxItem IsEnabled="True" Content="All">
<ComboBoxItem.Tag>
<system:Int32>0</system:Int32>
</ComboBoxItem.Tag>
</ComboBoxItem>
<CollectionContainer Collection="{Binding Source={StaticResource ResourceKey=ModulesCombo}}" />
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
现在 source 只是一个整数列表,但我需要一些默认值,所以我添加了 ComboBoxItem。现在选定的项目属性如下所示:
private int selectedModule;
public int SelectedAModule
{
get => selectedModule;
set
{
selectedModule = value;
OnPropertyChanged();
}
}
当更改为默认值时,我得到了
System.Windows.Data 错误:23:无法将“System.Windows.Controls.ComboBoxItem: All”从“ComboBoxItem”类型转换为“System.Int32”类型
如果我检查命令:
public ICommand GetCurrentsModuleCommand => new Command(module =>
{
SomeLogic();
});
如果是正常值模块是一个数字,但如果它的全部值我得到:
"System.Windows.Controls.ComboBoxItem: All";
有没有办法解决这个问题?
【问题讨论】: