0
votes

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>
1
Why you not simply check if value is not null before doing the cast? - Alberto
What exactly do you mean when you say 'when i switch content to a different content'? - Sheridan
@Sheridan my application has a ContentControl that displays different UserControls, each with an acoompanying ViewModel. These are bound to the ContentControl's Content property. When the ContentControl Content changes from the View/VM with the above ComboBox to a different View/VM (or the ContentControl Content is set to null), i hit the breakpoint in the Convert moethod of the Converter. I want to know WHY this happens. - Theodosius Von Richthofen

1 Answers

1
votes

How about this?:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    if (value.GetType() != typeof(int)) return false; // <--------
    string answer = "";
    switch ((int)value)
    {
        case 1:
        {
            answer = DatesInfo.Instance.MonthsNames[0];
            break;
        }            
        default:
        {
            break;
        }
    }   
    return answer;
}

UPDATE >>>

Ok, so you want to know why you receive a null value in your Converter class... well in short, I can't answer that. However, I have a WPF application with an interchangeable view model bound to a ContentControl in the same way that you described, so I did a little experiment.

I have a DebugConverter which is basically an empty Converter class that simply returns the input value unchanged. I can hook this into any Binding and put a break point in it to check the values that are being passed. So I hooked it into my interchangeable view model Binding and even after several view changes, at no point was the input value null.

So to find out why you are getting null values could be tricky without you providing more code, possibly even that within the MVVM Light Toolkit source code. Perhaps it would be simpler to accept that it happens and program to negate that fact, eg. with a check for null.