This is a migrated thread and some comments may be shown as answers.

Set combobox to nothing selected

2 Answers 2263 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Alex
Top achievements
Rank 1
Alex asked on 31 Jan 2012, 05:52 PM
Hello,

I am trying to clear the selection in a combobox by setting the bound object on the binding to null. This however does not make the combobox go blank as required.

I know there is a button you can have displayed on the combobox so the user can clear the selection but I need the functionality through the binding.

Is there a way to do this please?

Thanks

2 Answers, 1 is accepted

Sort by
0
Yana
Telerik team
answered on 06 Feb 2012, 12:24 PM
Hi Alex,

Setting SelectedItem property of the combobox to Null should clear the selection. I've attached a simple project to demonstrate the approach.

All the best,
Yana
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Alex
Top achievements
Rank 1
answered on 28 Mar 2012, 10:02 AM
Hi Yana,

Thanks for the sample. Unfortunately, it seemed that whenever we set the selected value or item to null, we got an error (Value '' could not be converted). I can understand this behaviour as the combobox had no corresponding null entry. In fact, this error occurred even with the RadComboBox's ClearSelectionButton.

We eventually got around this by using a converter on the SelectedValue binding of the control. The converter sets the value to -1 if a null is passed in. This works in our instance as the object is initialised with an ID (which is what the SelectedValuePath is set to) of -1.
public class ComboBoxNullConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }
 
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
            return -1;
        return value;
    }
}

Tags
ComboBox
Asked by
Alex
Top achievements
Rank 1
Answers by
Yana
Telerik team
Alex
Top achievements
Rank 1
Share this question
or