Category / Section
How to select items from ComboBoxAdv control at StartUp
1 min read
How to select items from ComboBoxAdv control at StartUp?
When AllowMultiSelect is true, the SelectedItems property exposes the items that are selected in the drop down list. In the below example, first two items from the Observable Collection bound to the SelectedItems property.
Refer to the following code example
MainWindow.xaml
<Window.DataContext> <local:ViewModel/> </Window.DataContext> <Grid> <syncfusion:ComboBoxAdv DisplayMemberPath="Name" SelectedItems="{Binding SelectedItems, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" AllowMultiSelect="True" Name="comboboxadv" HorizontalAlignment="Center" Height="25" VerticalAlignment="Center" Width="400" ItemsSource="{Binding Products}"/> </Grid>
Model.cs
public class Model { public string Name { get; set; } }
ViewModel.cs
public class ViewModel : INotifyPropertyChanged { private ObservableCollection<object> _allItems; private ObservableCollection<object> selectedItems; public ObservableCollection<object> SelectedItems { get { return selectedItems; } set { selectedItems = value; RaisePropertyChanged("SelectedItems"); } } private ObservableCollection<Model> products; public ObservableCollection<Model> Products { get { return products; } set { products = value; } } public ViewModel() { Products = new ObservableCollection<Model>(); Products.Add(new Model() { Name = "UK" }); Products.Add(new Model() { Name = "CA " }); Products.Add(new Model() { Name = "DE" }); Products.Add(new Model() { Name = "IN" }); Products.Add(new Model() { Name = "UA" }); _allItems = new ObservableCollection<object>(); for (int i = 0; i < 2; i++) { _allItems.Add(Products[i]); } SelectedItems = new ObservableCollection<object>(_allItems); } public event PropertyChangedEventHandler PropertyChanged; public void RaisePropertyChanged(string propertyName) { var property = PropertyChanged; if (property != null) property(this, new PropertyChangedEventArgs(propertyName)); } }
The following screenshot displays the ComboBoxAdv control with selected items