My integer to month converter seems to work fine for displaying the month name in a combobox when my source only has the integer month value. However, my program uses messaging from MVVMLight to swap out content so i don't have so many windows. Why does the Convert method get called when i switch content to a different content? Normally, the converter gets an integer and converts it to a string. But upon swapping the content, it receives empty string as the value, therefore producing the InvalidCastException. How can i fix? I included a seriously dumbed down code that simulates this just be setting Content=null after pressing a button. Thanks! The items source is bound to a singelton list of ints to represent the months.
Basically:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string answer = "";
switch ((int)value)
{
case 1:
{
answer = DatesInfo.Instance.MonthsNames[0];
break;
}
default:
{
break;
}
}
return answer;
}
XAML:
<ComboBox Width="200" Height="30" SelectedItem="{Binding SelectedItem.Month}"
ItemsSource="{Binding MonthsValues, Source={x:Static local:DatesInfo.Instance}}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource MonthNameConverter}}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
valueis not null before doing the cast? - Alberto