Dav*_*man 4 data-binding wpf xaml combobox
我有一个组合框,其中包含我尝试绑定到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>
Run Code Online (Sandbox Code Playgroud)
这是我的问题:当我运行应用程序时,组合项目加载正常.但是当我从列表中选择一个项目时,我在"输出"窗口中收到此错误:
Int32Converter cannot convert from System.Windows.Controls.ComboBoxItem.
Run Code Online (Sandbox Code Playgroud)
窗口中的其他数据绑定工作正常.完整的错误消息将在下面重新打印.
什么是告诉我的错误消息,我该如何解决这个问题?在此先感谢您的帮助.
完整的错误消息:
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)'
Run Code Online (Sandbox Code Playgroud)
H.B*_*.B. 10
SelectedValuePath到Content,这将使SelectedValue在Content选定的ComboBoxItem.SelectedValue到VM的属性而不是SelectedItem.<ComboBox
Width="60"
Margin="2,0,3,0"
SelectedValuePath="Content"
SelectedValue="{Binding FontSize}">
Run Code Online (Sandbox Code Playgroud)
(设置UpdateSourceTrigger和Mode不需要,因为它们默认设置为这样)
| 归档时间: |
|
| 查看次数: |
2730 次 |
| 最近记录: |