Integers in a combo box are not bound to data

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

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

Here is my problem: when I run the application, combo elements load normally. 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. 

Other data bindings in the window work fine. The full error message is reprinted below.

What is the error message telling me, and how can I fix it? 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)' 
+4
source share
2 answers

Your ComboBox contains ComboBoxItems, and you have bound the selected item to an integer property that causes errors. When you select ComboBoxItem, it tries to set ComboBoxItem as SelectedItem, but throws an error when trying to pass it as an integer. I would associate a set of integers in your view model with ComboBox.ItemsSource to fix this. This option gives you the flexibility to read a list from a database or file.

 <ComboBox SelectedItem="{Binding Path=FontSize, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="60" Margin="2,0,3,0" ItemsSource="{Binding FontSizeList}"/> 
+4
source
  • Set SelectedValuePath to Content , this will make the SelectedValue Content selected ComboBoxItem .
  • Bind SelectedValue to the VM property instead of SelectedItem .
 <ComboBox Width="60" Margin="2,0,3,0" SelectedValuePath="Content" SelectedValue="{Binding FontSize}"> 

( UpdateSourceTrigger and Mode settings UpdateSourceTrigger not required as they are set by default)

+8
source

All Articles