【问题标题】:Integers in combo box won't databind组合框中的整数不会数据绑定
【发布时间】: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


    【解决方案1】:
    1. 将SelectedValuePath 设置为Content,这将使SelectedValue 成为所选Content 的Content。
    2. 将SelectedValue 绑定到VM 的属性,而不是SelectedItem。
    <ComboBox
        Width="60"
        Margin="2,0,3,0"
        SelectedValuePath="Content"
        SelectedValue="{Binding FontSize}">
    

    (不需要设置UpdateSourceTrigger 和Mode,因为它们默认是这样设置的)

    【讨论】:

    • 我看到你发布了许多问题的答案。你靠什么谋生才能有时间回答所有这些问题?
    • @evanb:哈哈,我是一名大学生(学习计算机科学和媒体),目前有一个学期的假期。诚然,即使在学期期间,我也花了很多时间在 SO 上,实际上可能太多了。编辑:哇,我的回答不再“有见地”了?
    • 这很有趣,我尝试回答了几个问题,发现您在问题发布后的几分钟内就发布了其中许多问题的答案。您绝对是一个知识渊博的人,并且是 SO 社区的重要成员。我正在阅读您在我的答案上写的评论,并认为有些人可能会认为您的答案没有洞察力,所以我听从了您的建议,哈哈。
    • @KushalR:不是真的。我也非常喜欢绑定到数据并在适用的情况下使用数据模板。这个解决方案不是我通常会使用的,我自己很少实例化像ComboBoxItem 这样的项目容器。
    【解决方案2】:

    您的 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}"/>
    

    【讨论】:

    • 解决这个问题的方法不止两种,做出这样的陈述绝不是一个好主意。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    • 2012-01-24
    • 1970-01-01
    • 1970-01-01
    • 2017-11-16
    • 2013-04-23
    相关资源
    最近更新 更多