|
1 | | -# WPF-ComboBoxAdv-MultiSelection |
2 | | -This repository contains the sample that how to select the items programmatically in WPF ComboBoxAdv. |
| 1 | +# WPF ComboBoxAdv MultiSelection |
| 2 | +This repository demonstrates how to select items programmatically in the Syncfusion WPF ComboBoxAdv control and enable multi-selection functionality. |
| 3 | + |
| 4 | +## Why Use ComboBoxAdv MultiSelection? |
| 5 | +- Allows users to select multiple items from a dropdown list. |
| 6 | +- Supports data binding for dynamic item updates. |
| 7 | +- Ideal for scenarios like filtering, tagging, or multi-choice forms. |
| 8 | + |
| 9 | +## Creating project |
| 10 | +Follow these steps to create a new WPF project in Visual Studio and add the ComboBoxAdv control: |
| 11 | + |
| 12 | +### Adding Control Manually in XAML |
| 13 | + |
| 14 | +1. Add the required assembly reference: |
| 15 | + - Syncfusion.Shared.WPF |
| 16 | + |
| 17 | +2. Import the Syncfusion WPF schema: |
| 18 | +```XAML |
| 19 | +xmlns:syncfusion="http://schemas.syncfusion.com/wpf" |
| 20 | +``` |
| 21 | + |
| 22 | +3. Declare the ComboBoxAdv in XAML: |
| 23 | +```XAML |
| 24 | +<Grid> |
| 25 | + <syncfusion:ComboBoxAdv Height="30" Width="150"/> |
| 26 | +</Grid> |
| 27 | +``` |
| 28 | + |
| 29 | +### Adding Control Manually in C# |
| 30 | + |
| 31 | +1. Add the required assembly reference: |
| 32 | + - Syncfusion.Shared.WPF |
| 33 | + |
| 34 | +2. Import the namespace: |
| 35 | +```C# |
| 36 | +using Syncfusion.Windows.Tools.Controls; |
| 37 | +``` |
| 38 | + |
| 39 | +3. Create and configure the ComboBoxAdv instance: |
| 40 | +```C# |
| 41 | +public partial class MainWindow : Window |
| 42 | +{ |
| 43 | + public MainWindow() |
| 44 | + { |
| 45 | + InitializeComponent(); |
| 46 | + ComboBoxAdv comboBoxAdv = new ComboBoxAdv |
| 47 | + { |
| 48 | + Height = 30, |
| 49 | + Width = 150, |
| 50 | + DefaultText = "Choose Items" |
| 51 | + }; |
| 52 | + this.Content = comboBoxAdv; |
| 53 | + } |
| 54 | +} |
| 55 | +``` |
| 56 | +## ComboBoxAdv MultiSelection Example |
| 57 | +Enable multi-selection and bind data using MVVM: |
| 58 | + |
| 59 | +**[XAML]** |
| 60 | +```XAML |
| 61 | +<Window.DataContext> |
| 62 | + <local:ViewModel /> |
| 63 | +</Window.DataContext> |
| 64 | + |
| 65 | +<Grid> |
| 66 | + <syncfusion:ComboBoxAdv DisplayMemberPath="Name" |
| 67 | + SelectedItems="{Binding SelectedItems, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" |
| 68 | + AllowMultiSelect="True" |
| 69 | + Name="comboboxadv" |
| 70 | + HorizontalAlignment="Center" |
| 71 | + Height="30" |
| 72 | + VerticalAlignment="Center" |
| 73 | + Width="150" |
| 74 | + ItemsSource="{Binding Countries}"/> |
| 75 | +</Grid> |
| 76 | +``` |
| 77 | +**ViewModel** |
| 78 | +```C# |
| 79 | +public class ViewModel : INotifyPropertyChanged |
| 80 | +{ |
| 81 | + private ObservableCollection<object> selectedItems; |
| 82 | + public ObservableCollection<object> SelectedItems |
| 83 | + { |
| 84 | + get => selectedItems; |
| 85 | + set |
| 86 | + { |
| 87 | + selectedItems = value; |
| 88 | + RaisePropertyChanged(nameof(SelectedItems)); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public ObservableCollection<Country> Countries { get; set; } |
| 93 | + |
| 94 | + public ViewModel() |
| 95 | + { |
| 96 | + Countries = new ObservableCollection<Country> |
| 97 | + { |
| 98 | + new Country() { Name = "Denmark" }, |
| 99 | + new Country() { Name = "New Zealand" }, |
| 100 | + new Country() { Name = "Canada" }, |
| 101 | + new Country() { Name = "Russia" }, |
| 102 | + new Country() { Name = "Japan" } |
| 103 | + }; |
| 104 | + |
| 105 | + SelectedItems = new ObservableCollection<object>(Countries.Take(2)); |
| 106 | + } |
| 107 | + |
| 108 | + public event PropertyChangedEventHandler PropertyChanged; |
| 109 | + private void RaisePropertyChanged(string propertyName) |
| 110 | + => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); |
| 111 | +} |
| 112 | + |
| 113 | +public class Country |
| 114 | +{ |
| 115 | + public string Name { get; set; } |
| 116 | +} |
| 117 | +``` |
0 commit comments