开发者

Integers in combo box won't databind

I have a combo box with a list of font sizes that I am trying to bind to an MVVM view model. The SelectedItem property is bound to my Viwe model property, FontSize. The drop-down list is fixed, so I declare the combo box items in the XAML for the combo box, like this:

    <ComboBo开发者_如何学JAVAx 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>

Here is my problem: When I run the app, the combo items load fine. But when I select an item from the list, I get this error in the Output window:

Int32Converter cannot convert from System.Windows.Controls.ComboBoxItem.

The other data bindings in the window are working fine. The full error message is reprinted below.

What is the error message telling me, and how do I fix this? Thanks in advance for your help.


Full error message:

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)'


  1. Set the SelectedValuePath to Content, this will make the SelectedValue the Content of the selected ComboBoxItem.
  2. Bind the SelectedValue to your VM's property instead of the SelectedItem.
<ComboBox
    Width="60"
    Margin="2,0,3,0"
    SelectedValuePath="Content"
    SelectedValue="{Binding FontSize}">

(Setting UpdateSourceTrigger and the Mode is not needed as they are set that way by default)


Your ComboBox contains ComboBoxItems and you bound the selectedItem to an integer property which is causing the errors. When you select a ComboBoxItem it tries to set the ComboBoxItem as the SelectedItem but throws an error when trying to cast it as an integer. I would bind a collection of integers in your view model to the ComboBox.ItemsSource to fix this. This option gives you the flexibility of reading the list from a database or a file.

<ComboBox SelectedItem="{Binding Path=FontSize, Mode=TwoWay, 
                         UpdateSourceTrigger=PropertyChanged}" 
          Width="60" Margin="2,0,3,0" ItemsSource="{Binding FontSizeList}"/>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜