【发布时间】:2011-08-23 19:36:20
【问题描述】:
我有一个组合框,其中包含我试图绑定到 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)'
【问题讨论】:
标签: wpf xaml data-binding combobox