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