|
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 | 3 |
|
4 | | -# Creating project |
5 | | -Below section provides detailed information to create new project in Visual Studio to display ComboBoxAdv. |
6 | | -# Adding control manually in XAML |
7 | | -In order to add ComboBoxAdv control manually in XAML, do the below steps, |
| 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 | 8 |
|
9 | | -1. Add the below required assembly references to the project, |
| 9 | +## Creating project |
| 10 | +Follow these steps to create a new WPF project in Visual Studio and add the ComboBoxAdv control: |
10 | 11 |
|
11 | | - * Syncfusion.Shared.WPF |
12 | | -2. Import Syncfusion WPF schema http://schemas.syncfusion.com/wpf in XAML page or Syncfusion.Windows.Tools.Controls namespace. |
| 12 | +### Adding Control Manually in XAML |
13 | 13 |
|
14 | | -3. Declare ComboBoxAdv in XAML page. |
15 | | -**[XAML]** |
| 14 | +1. Add the required assembly reference: |
| 15 | + - Syncfusion.Shared.WPF |
16 | 16 |
|
| 17 | +2. Import the Syncfusion WPF schema: |
| 18 | +```XAML |
| 19 | +xmlns:syncfusion="http://schemas.syncfusion.com/wpf" |
17 | 20 | ``` |
18 | | - <Grid> |
19 | | - <syncfusion:ComboBoxAdv Height="30" Width="150"/> |
20 | | - </Grid> |
21 | | -``` |
22 | | - |
23 | | -# Adding control manually in C# |
24 | | -In order to add ComboBoxAdv control manually in C#, do the below steps, |
25 | | - |
26 | | -1. Add the below required assembly references to the project, |
27 | | - |
28 | | - * Syncfusion.Shared.WPF |
29 | 21 |
|
30 | | -2. Import ComboBoxAdv namespace Syncfusion.Windows.Tools.Controls. |
| 22 | +3. Declare the ComboBoxAdv in XAML: |
| 23 | +```XAML |
| 24 | +<Grid> |
| 25 | + <syncfusion:ComboBoxAdv Height="30" Width="150"/> |
| 26 | +</Grid> |
| 27 | +``` |
31 | 28 |
|
32 | | -3. Create ComboBoxAdv control instance and add it to the page. |
| 29 | +### Adding Control Manually in C# |
33 | 30 |
|
| 31 | +1. Add the required assembly reference: |
| 32 | + - Syncfusion.Shared.WPF |
34 | 33 |
|
35 | | -**[C#]** |
| 34 | +2. Import the namespace: |
| 35 | +```C# |
| 36 | +using Syncfusion.Windows.Tools.Controls; |
36 | 37 | ``` |
| 38 | + |
| 39 | +3. Create and configure the ComboBoxAdv instance: |
| 40 | +```C# |
37 | 41 | public partial class MainWindow : Window |
38 | 42 | { |
39 | 43 | public MainWindow() |
40 | 44 | { |
41 | 45 | InitializeComponent(); |
42 | | - ComboBoxAdv comboBoxAdv = new ComboBoxAdv(); |
| 46 | + ComboBoxAdv comboBoxAdv = new ComboBoxAdv |
| 47 | + { |
| 48 | + Height = 30, |
| 49 | + Width = 150, |
| 50 | + DefaultText = "Choose Items" |
| 51 | + }; |
43 | 52 | this.Content = comboBoxAdv; |
44 | | - comboBoxAdv.Height = 30; |
45 | | - comboBoxAdv.Width = 150; |
46 | | - comboBoxAdv.DefaultText = "choose Items"; |
47 | 53 | } |
48 | 54 | } |
49 | 55 | ``` |
50 | | -# ComboBoxAdv MultiSelection |
| 56 | +## ComboBoxAdv MultiSelection Example |
| 57 | +Enable multi-selection and bind data using MVVM: |
51 | 58 |
|
52 | 59 | **[XAML]** |
53 | | - |
54 | | -``` |
55 | | -<syncfusion:ComboBoxAdv DisplayMemberPath="Name" |
56 | | - SelectedItems="{Binding SelectedItems, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" |
57 | | - AllowMultiSelect="True" Name="comboboxadv" HorizontalAlignment="Center" Height="30" |
58 | | - VerticalAlignment="Center" Width="150" ItemsSource="{Binding Countries}"/> |
| 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> |
59 | 76 | ``` |
60 | | -## How to run this application? |
| 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 | + } |
61 | 91 |
|
62 | | -To run this application, you need to first clone the WPF-ComboBoxAdv-MultiSelection repository and then open it in Visual Studio 2022. Now, simply build and run your project to view the output. |
| 92 | + public ObservableCollection<Country> Countries { get; set; } |
63 | 93 |
|
64 | | -## <a name="troubleshooting"></a>Troubleshooting ## |
65 | | -### Path too long exception |
66 | | -If you are facing path too long exception when building this example project, close Visual Studio and rename the repository to short and build the project. |
| 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 | + } |
67 | 107 |
|
68 | | -## License |
| 108 | + public event PropertyChangedEventHandler PropertyChanged; |
| 109 | + private void RaisePropertyChanged(string propertyName) |
| 110 | + => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); |
| 111 | +} |
69 | 112 |
|
70 | | -Syncfusion has no liability for any damage or consequence that may arise by using or viewing the samples. The samples are for demonstrative purposes, and if you choose to use or access the samples, you agree to not hold Syncfusion liable, in any form, for any damage that is related to use, for accessing, or viewing the samples. By accessing, viewing, or seeing the samples, you acknowledge and agree Syncfusion’s samples will not allow you seek injunctive relief in any form for any claim related to the sample. If you do not agree to this, do not view, access, utilize, or otherwise do anything with Syncfusion’s samples. |
| 113 | +public class Country |
| 114 | +{ |
| 115 | + public string Name { get; set; } |
| 116 | +} |
| 117 | +``` |
0 commit comments