MultiSelection Support in WPF ComboBox (ComboBoxAdv)

6 Nov 2020 / 5 minutes to read

This section explains how to select the multiple items and select the items programmatically in the ComboBoxAdv control.

Properties

Property Description Type Data Type Reference links
AllowMultiSelect Multiple items can be selected. Dependency Property Boolean NA
SelectedItems It contains the selected items value Dependency Property ObservableCollection<object> NA

Adding multiple selections to an application

You can select the multiple items in the WPF ComboBoxAdv control by setting the AllowMultiSelect property to true.

<syncfusion:ComboBoxAdv x:Name="comboBoxAdv" AllowMultiSelect="True">
</syncfusion:ComboBoxAdv>
ComboBoxAdv comboBoxAdv = new ComboBoxAdv();
comboBoxAdv.AllowMultiSelect = true;

Selecting an item through programmatically

You can select the items programmatically by using the SelectedItems property. When AllowMultiSelect is set to 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.

Creating Model and ViewModel data for DataBinding

1.Create a data object class named Country and declare the property as follows.

public class Country
{
    public string Name { get; set; }
}

2.Create a ViewModel class with SelectedItems, which are initialized with data objects in constructor.

public class ViewModel : INotifyPropertyChanged
{
    private ObservableCollection<object> _items;

    private ObservableCollection<object> selectedItems;
    public ObservableCollection<object> SelectedItems
    {
        get { return selectedItems; }
        set
        {
            selectedItems = value;
            RaisePropertyChanged("SelectedItems");
        }
    }
    private ObservableCollection<Country> countries;
    public ObservableCollection<Country> Countries
    {
        get { return countries; }
        set
        {
            countries = value;
        }
    }
    public ViewModel()
    {
        Countries = new ObservableCollection<Country>();
        Countries.Add(new Country() { Name = "Denmark" });
        Countries.Add(new Country() { Name = "New Zealand" });
        Countries.Add(new Country() { Name = "Canada" });
        Countries.Add(new Country() { Name = "Russia" });
        Countries.Add(new Country() { Name = "Japan" });

        _items = new ObservableCollection<object>();
        for (int i = 0; i < 2; i++)
        {
            _items.Add(Countries[i]);
        }
        SelectedItems = new ObservableCollection<object>(_items);
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void RaisePropertyChanged(string PropertyName)
    {
        var property = PropertyChanged;
        if (property != null)
            property(this, new PropertyChangedEventArgs(PropertyName));
    }
}

3.To bind the ComboBoxAdv to data, bind the collection created in the previous step to the ItemsSource property in XAML by setting the ViewModel as DataContext.

<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="30"
            VerticalAlignment="Center" Width="150" ItemsSource="{Binding Products}"/>
</Grid>

Adding multiple selections to an application in WPF ComboBoxAdv

NOTE

View sample in GitHub