ComboBox with No Null Values
The following article will show you how to prevent item deselection when the ComboBox selection mode is single and there is initially selected item.
Here are the steps needed to achieve the functionality describe above.
To prevent item deselection we will need to set IsClearButtonvisible
to False
and SelectionMode
to Single
Here is the ComboBox definition in XAML
<telerikInput:RadComboBox Grid.Row="1"
ItemsSource="{Binding Items}"
SelectedItem="{Binding SelectedItem}"
DisplayMemberPath="Name"
IsClearButtonVisible="False"
SelectionMode="Single"
Margin="10, 0, 10, 0">
<telerikInput:RadComboBox.BindingContext>
<local:ViewModel/>
</telerikInput:RadComboBox.BindingContext>
</telerikInput:RadComboBox>
the business model used:
public class City
{
public string Name { get; set; }
public int Population { get; set; }
}
Mainly inside the ViewModel you will need to declare a property and bind it to the ComboBox SelectedItem
property, then set a value for this property in the ViewModel's contructor:
public class ViewModel : NotifyPropertyChangedBase
{
private City selectedItem;
public ViewModel()
{
this.Items = new ObservableCollection<City>
{
new City { Name = "Tokyo", Population = 13929286 },
new City { Name = "New York", Population = 8623000 },
new City { Name = "London", Population = 8908081 },
new City { Name = "Madrid", Population = 3223334 },
new City { Name = "Los Angeles", Population = 4000000},
new City { Name = "Paris", Population = 2141000 },
new City { Name = "Beijing", Population = 21540000 },
new City { Name = "Singapore", Population = 5612000 },
new City { Name = "New Delhi", Population = 18980000 },
new City { Name = "Bangkok", Population = 8305218 },
new City { Name = "Berlin", Population = 3748000 },
};
this.SelectedItem = this.Items[0];
}
public ObservableCollection<City> Items { get; set; }
public City SelectedItem
{
get
{
return this.selectedItem;
}
set
{
if (this.selectedItem != value)
{
this.selectedItem = value;
this.OnPropertyChanged();
}
}
}
}
Example for ComboBox No Null Value can be found in the ComboBox/How To section from the SDK Browser Application.