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

Prevent SelectedValue from being NULL with RadComboBox

3 Answers 292 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Brian Sayatovic
Top achievements
Rank 1
Brian Sayatovic asked on 02 May 2012, 08:35 PM
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?

3 Answers, 1 is accepted

Sort by
0
Yana
Telerik team
answered on 03 May 2012, 08:22 AM
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 >>

0
Brian Sayatovic
Top achievements
Rank 1
answered on 03 May 2012, 01:15 PM
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.
0
Yana
Telerik team
answered on 07 May 2012, 09:46 AM
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 >>

Kunal
Top achievements
Rank 1
commented on 16 Jun 2023, 06:04 AM

How can I do that in ViewModel can you describe that.

 

 
Martin Ivanov
Telerik team
commented on 20 Jun 2023, 11:52 AM

I believe that Yana had something like this in mind:

private string id;
public string Id
{
	get { return id; }
	set
	{
		string newId = value;
		if (string.IsNullOrEmpty(newId))
		{
			newId = "myDefaultValueHere";
		}
		id = newId;
		OnPropertyChanged(nameof(Id));
	}
}

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