Introduction:

Recently, in our project we wanted to allow the user to select multiple values in a list. But the list should be populated inside a grid row. So we don't want to use a listbox and also we are not interested in third-party tools. Instead of that, we wanted to use a multiselect combobox. When I browsed through various blogs, forums, etc. I got some good code, but none of them are easy for me to understand and implement so I decided to move with something bit tricky but easy for you. In short, below you will find a MultiSelect Combobox with Search feature on elements.

Using the code:

In the the below screen shots you will see I used a Listbox control to demonstrate the selected elements in Multiselect ComboBox.

main

In the below image you will see how the search feature is working; it doesn’t affect the selected elements but will manage the display of the elements; when search text was removed all Selected elements will come back into the List.

list

For better understanding I am going to split the article into two parts now :

  1. XML or designing Part
  2. C# part

To define the design part I used two controls, Dropdown for selection, and one Listbox just to display selected countries.

  1. <ComboBox Height="30" TextBoxBase.TextChanged="ddlCountry_TextChanged" HorizontalAlignment="Left" VerticalAlignment="Top" Name="ddlCountry" IsEditable="True" IsTextSearchEnabled="True" StaysOpenOnEdit="True" Width="165" SelectionChanged="ddlCountry_SelectionChanged" Margin="78,139,0,0">
  2. <ComboBox.ItemTemplate>
  3. <DataTemplate>
  4. <CheckBox Name="chkCountry" Width="220" Checked="AllCheckbocx_CheckedAndUnchecked" Unchecked="AllCheckbocx_CheckedAndUnchecked" Content="{Binding Country_Name}" IsChecked="{Binding Check_Status}" CommandParameter="{Binding Country_ID}">
  5. </CheckBox>
  6. </DataTemplate>
  7. </ComboBox.ItemTemplate>
  8. </ComboBox>
  9. <ListBox Name="testListbox" HorizontalAlignment="Left" Height="161" Margin="370,80,0,0" VerticalAlignment="Top" Width="100" />
To Bind the Multiselect Combobox ddlCountry I created a class with three properties. In the the Check_Status is basically used to identify selected items.
  1. public class DDL_Country
  2. {
  3. public int Country_ID
  4. {
  5. get;
  6. set;
  7. }
  8. public string Country_Name
  9. {
  10. get;
  11. set;
  12. }
  13. public Boolean Check_Status
  14. {
  15. get;
  16. set;
  17. }
  18. }
To Bind country list in Dropdown I created one method.
  1. private void AddElementsInList()
  2. {
  3. // 1 element
  4. DDL_Country obj = new DDL_Country();
  5. obj.Country_ID = 10;
  6. obj.Country_Name = "India";
  7. objCountryList.Add(obj);
  8. obj = new DDL_Country();
  9. obj.Country_ID = 11;
  10. obj.Country_Name = "Pakistan";
  11. objCountryList.Add(obj);
  12. obj = new DDL_Country();
  13. obj.Country_ID = 12;
  14. obj.Country_Name = "America";
  15. objCountryList.Add(obj);
  16. obj = new DDL_Country();
  17. obj.Country_ID = 13;
  18. obj.Country_Name = "U.K";
  19. objCountryList.Add(obj);
  20. obj = new DDL_Country();
  21. obj.Country_ID = 14;
  22. obj.Country_Name = "Arab";
  23. objCountryList.Add(obj);
  24. }
Please Find below the whle CS page code.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Linq;
  6. namespace MultiSelectCombobox
  7. {
  8. /// <summary>
  9. /// Interaction logic for MainWindow.xaml
  10. /// </summary>
  11. public partial class MainWindow: Window
  12. {
  13. List < DDL_Country > objCountryList;
  14. public MainWindow()
  15. {
  16. InitializeComponent();
  17. objCountryList = new List < DDL_Country > ();
  18. AddElementsInList();
  19. BindCountryDropDown();
  20. }
  21. private void BindCountryDropDown()
  22. {
  23. ddlCountry.ItemsSource = objCountryList;
  24. }
  25. private void ddlCountry_SelectionChanged(object sender, SelectionChangedEventArgs e)
  26. {
  27. }
  28. private void ddlCountry_TextChanged(object sender, TextChangedEventArgs e)
  29. {
  30. ddlCountry.ItemsSource = objCountryList.Where(x => x.Country_Name.StartsWith(ddlCountry.Text.Trim()));
  31. }
  32. private void AllCheckbocx_CheckedAndUnchecked(object sender, RoutedEventArgs e)
  33. {
  34. BindListBOX();
  35. }
  36. private void BindListBOX()
  37. {
  38. testListbox.Items.Clear();
  39. foreach(var country in objCountryList)
  40. {
  41. if (country.Check_Status == true)
  42. {
  43. testListbox.Items.Add(country.Country_Name);
  44. }
  45. }
  46. }
  47. private void AddElementsInList()
  48. {
  49. // 1 element
  50. DDL_Country obj = new DDL_Country();
  51. obj.Country_ID = 10;
  52. obj.Country_Name = "India";
  53. objCountryList.Add(obj);
  54. obj = new DDL_Country();
  55. obj.Country_ID = 11;
  56. obj.Country_Name = "Pakistan";
  57. objCountryList.Add(obj);
  58. obj = new DDL_Country();
  59. obj.Country_ID = 12;
  60. obj.Country_Name = "America";
  61. objCountryList.Add(obj);
  62. obj = new DDL_Country();
  63. obj.Country_ID = 13;
  64. obj.Country_Name = "U.K";
  65. objCountryList.Add(obj);
  66. obj = new DDL_Country();
  67. obj.Country_ID = 14;
  68. obj.Country_Name = "Arab";
  69. objCountryList.Add(obj);
  70. }
  71. }
  72. public class DDL_Country
  73. {
  74. public int Country_ID
  75. {
  76. get;
  77. set;
  78. }
  79. public string Country_Name
  80. {
  81. get;
  82. set;
  83. }
  84. public Boolean Check_Status
  85. {
  86. get;
  87. set;
  88. }
  89. }
  90. }