Set combobox to nothing selected

3 posts, 0 answers
  1. Alex
    Alex avatar
    17 posts
    Member since:
    Jan 2012

    Posted 31 Jan 2012 Link to this post

    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. Yana
    Admin
    Yana avatar
    4554 posts

    Posted 06 Feb 2012 Link to this post

    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 >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Alex
    Alex avatar
    17 posts
    Member since:
    Jan 2012

    Posted 28 Mar 2012 Link to this post

    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;
        }
    }

Back to Top