Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WPF > ComboBox > Set combobox to nothing selected

Not answered Set combobox to nothing selected

Feed from this thread
  • Alex avatar

    Posted on Jan 31, 2012 (permalink)

    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

    Reply

  • Yana Yana admin's avatar

    Posted on Feb 6, 2012 (permalink)

    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 >>
    Attached files

    Reply

  • Alex avatar

    Posted on Mar 28, 2012 (permalink)

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

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WPF > ComboBox > Set combobox to nothing selected
Related resources for "Set combobox to nothing selected"

WPF ComboBox Features  |  Documentation  |  Demos  |  Telerik TV  |  Self-Paced Trainer  ]