Prevent SelectedValue from being NULL with RadComboBox

4 posts, 0 answers
  1. Brian Sayatovic
    Brian Sayatovic avatar
    91 posts
    Member since:
    Nov 2009

    Posted 02 May 2012 Link to this post

    I have a combobox, shown below.  The MyItems collection has a series of states with verbose display names like "State of Alaska", "State of Alabama", etc.  I want the selected value -- bound to MyModel.ItemId -- to NEVER be null.
    <telerik:RadComboBox x:Name="MyComboBox"
            IsReadOnly="True"
            IsEditable="True"
            CanAutocompleteSelectItems="False"
            IsFilteringEnabled="True"
            TextSearchMode="Contains"
            StaysOpenOnEdit="True"
            OpenDropDownOnFocus="True"
            ItemsSource="{Binding MyItems}"
            DisplayMemberPath="DisplayName"
            SelectedValuePath="Id"
            SelectedValue="{Binding MyModel.ItemId, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnDataErrors=true}"
            />

    When the view is loaded, the Id starts out as not-null (defaulted by server long before), e.g. "Territory of Puerto Rico".  When I focus the box, the dropdown opens with "Territory of Puerto Rico" selected.  I can start backspacing just fine.  But when I backspace all the way to blank, it's updating my SelectedValue with NULL.

    How can I make the RadComboBox to require one of the items to be selected and NOT update the binding with NULL?
  2. Yana
    Admin
    Yana avatar
    4554 posts

    Posted 03 May 2012 Link to this post

    Hi Brian,

    You can implement validation using IDataErrorInfo interface, the approach is demonstrated in the following example:
    http://demos.telerik.com/silverlight/#ComboBox/Validation

    All the best,
    Yana
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft banner
  4. Brian Sayatovic
    Brian Sayatovic avatar
    91 posts
    Member since:
    Nov 2009

    Posted 03 May 2012 Link to this post

    Unfortunately, that example simply highlights it in red using the standard validation techniques.  I don't want to simply tell the user when they've selected nothing/blank/null... I want to PREVENT IT from being nothing/blank/null.  Binding a null back to my model is problematic for me.  I'm investigating what it would take to make my model more forgiving, but so far that's a ton of work.

    I was hoping the RadComboBox itself had a feature or trick to retain the previous value if someone tried to change it to nothing.  As it is, it seems the binding is updated as I type and it lands on other selected items, or null when I clear out the search text completely.
  5. Yana
    Admin
    Yana avatar
    4554 posts

    Posted 07 May 2012 Link to this post

    Hello Brian,

    RadComboBox doesn't provide such a feature. The only way is to check the value in the ViewModel and set a default value in case it's null.

    We're sorry for the inconvenience.

    Kind regards,
    Yana
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top