Question

I have a Silverlight page with a Telerik RadComboBox on it, and I have another element which I need to show/hide based on which item is selected in the ComboBox. What I want to do is bind the Visibility property of the hidden element so that it shows the appropriate item is selected.

In order to do this I created a very simple ValueConverter, and I am trying to bind the SelectedItem of the RadComboBox to that converter so that I can evaluate what item is selected, and return an appropriate Visibility. Easy peasy stuff really, but for some reason it is not working for me today. Maybe not enough caffeine...

The problem is, when the ValueConverter is called, the "value" being passed in is NULL.

I should also mention that this page is using Caliburn.Micro MVVM. Not sure if it makes a difference, but figured just in case...

Here is the ComboBox XAML:

<telerik:RadComboBox x:Name="RecordTypes" SelectedItem="{Binding Path=SelectedRecordType, Mode=TwoWay}" ItemsSource="{Binding Path=RecordTypes, Mode=TwoWay}" EmptyText="Select..." />

Here is the Binding on the other Element:

Visibility="{Binding Path=SelectedItem, ElementName=RecordTypes, Mode=TwoWay, Converter={StaticResource VisibilityConverter}}" 

Here is the (generic) converter. It is not yet setup to read the SelectedIndex value as the value being passed in is still NULL:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    bool visibility = (bool)value; <-- THIS VALUE IS NULL AND SHOULD NOT BE, IT SHOULD BE THE SelectedItem value
    return visibility ? Visibility.Visible : Visibility.Collapsed;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
    Visibility visibility = (Visibility)value;
    return (visibility == Visibility.Visible);
}

}

Was it helpful?

Solution

With the Telerik RadComboBox, the SelectedItem will be null (when nothing selected). My guess is that your converter is being called (with no selection made in your RadComboBox) and null is correctly being passed to your converter. I realize your converter is just a skeleton at the point, but you always need to handle null and data types in your converter... Example below.

Also, the value passed to your converter will be the same data type as the SelectedItem in your RadComboBox (whatever RecordTypes is a list of - probably not a list of bools?).

Example base boolToVis converter below.

public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {

        if (value == null || !(value is bool))
            return Visibility.Collapsed;

        bool booInvert = false;
        if (parameter is string) bool.TryParse((string)parameter, out booInvert);

        if (booInvert) return ((bool)value) ? Visibility.Collapsed :Visibility.Visible;
        else return ((bool)value) ? Visibility.Visible : Visibility.Collapsed;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top