我有一个组合框,其中包含我尝试绑定到MVVM视图模型的字体大小列表。 SelectedItem属性绑定到我的Viwe模型属性FontSize。下拉列表是固定的,因此我在组合框的XAML中声明了组合框项,如下所示:
<ComboBox SelectedItem="{Binding Path=FontSize, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
Width="60" Margin="2,0,3,0">
<ComboBoxItem Content="10" />
<ComboBoxItem Content="12" />
<ComboBoxItem Content="18" />
<ComboBoxItem Content="24" />
<ComboBoxItem Content="36" />
<ComboBoxItem Content="48" />
<ComboBoxItem Content="60" />
<ComboBoxItem Content="72" />
</ComboBox>
这是我的问题:当我运行应用程序时,组合项目加载正常。但是当我从列表中选择一个项目时,我在“输出”窗口中收到此错误:
Int32Converter cannot convert from System.Windows.Controls.ComboBoxItem.
窗口中的其他数据绑定工作正常。完整的错误消息将在下面重新打印。
告诉我的错误信息是什么,我该如何解决?在此先感谢您的帮助。
完整的错误消息:
System.Windows.Data Error: 23 : Cannot convert 'System.Windows.Controls.ComboBoxItem: 12' from type 'ComboBoxItem' to type 'System.Int32' for 'en-US' culture with default conversions; consider using Converter property of Binding. NotSupportedException:'System.NotSupportedException: Int32Converter cannot convert from System.Windows.Controls.ComboBoxItem.
at System.ComponentModel.TypeConverter.GetConvertFromException(Object value)
at System.ComponentModel.TypeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)'
System.Windows.Data Error: 7 : ConvertBack cannot convert value 'System.Windows.Controls.ComboBoxItem: 12' (type 'ComboBoxItem'). BindingExpression:Path=FontSize; DataItem='MainWindowViewModel' (HashCode=14640006); target element is 'ComboBox' (Name=''); target property is 'SelectedItem' (type 'Object') NotSupportedException:'System.NotSupportedException: Int32Converter cannot convert from System.Windows.Controls.ComboBoxItem.
at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)
at MS.Internal.Data.ObjectTargetConverter.ConvertBack(Object o, Type type, Object parameter, CultureInfo culture)
at System.Windows.Data.BindingExpression.ConvertBackHelper(IValueConverter converter, Object value, Type sourceType, Object parameter, CultureInfo culture)'
答案 0 :(得分:10)
SelectedValuePath
设置为Content
,这将使SelectedValue
成为所选Content
的{{1}}。ComboBoxItem
绑定到您的VM的属性而不是SelectedValue
。SelectedItem
(设置<ComboBox
Width="60"
Margin="2,0,3,0"
SelectedValuePath="Content"
SelectedValue="{Binding FontSize}">
并且不需要UpdateSourceTrigger
,因为它们默认设置为)
答案 1 :(得分:4)
您的ComboBox包含ComboBoxItems,并且您将selectedItem绑定到导致错误的整数属性。当您选择ComboBoxItem时,它会尝试将ComboBoxItem设置为SelectedItem,但在尝试将其强制转换为整数时会引发错误。我会将视图模型中的整数集合绑定到ComboBox.ItemsSource来解决此问题。此选项使您可以灵活地从数据库或文件中读取列表。
<ComboBox SelectedItem="{Binding Path=FontSize, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
Width="60" Margin="2,0,3,0" ItemsSource="{Binding FontSizeList}"/>