Multi Select Combobox in Silverlight With C#

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Linq;   
  4. using System.Net;   
  5. using System.Windows;   
  6. using System.Windows.Controls;   
  7. using System.Windows.Documents;   
  8. using System.Windows.Input;   
  9. using System.Windows.Media;   
  10. using System.Windows.Media.Animation;   
  11. using System.Windows.Shapes;   
  12. using Banthia_Mgt_System.Assets.Wrapper_Class;   
  13. using System.Collections.ObjectModel;   
  14. using Banthia_Mgt_System.Classes;   
  15. using Banthia_Mgt_System.Assets.Classes;   
  16.   
  17. namespace Banthia_Mgt_System.Assets.Multiselect_Combo   
  18. {   
  19.     public partial class UC_Multi_Dept_Combo : UserControl   
  20.     {   
  21.         List<CLS_Multi_Dept_Checked_WrapperBE> _Coll_Wrapper_BE;   
  22.         private List<CLS_Multi_Dept_Checked_WrapperBE> Coll_Wrapper_BE   
  23.         {   
  24.             get   
  25.             {   
  26.                 return _Coll_Wrapper_BE;   
  27.             }   
  28.             set   
  29.             {   
  30.                 _Coll_Wrapper_BE = value;   
  31.                 foreach (var item in Coll_Wrapper_BE)   
  32.                 item.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(item_PropertyChanged);   
  33.                 cmbDepartment.ItemsSource = value;   
  34.                 if (cmbDepartment.ItemsSource != null)   
  35.                     SetString();   
  36.             }   
  37.         }   
  38.   
  39.         public UC_Multi_Dept_Combo()   
  40.         {   
  41.             InitializeComponent();   
  42.             cmbDepartment.SelectionChanged += new SelectionChangedEventHandler(cmbDepartment_SelectionChanged);   
  43.             cmbDepartment.DropDownOpened += new EventHandler(cmbDepartment_DropDownOpened);   
  44.         }   
  45.   
  46.         void cmbDepartment_DropDownOpened(object sender, EventArgs e)   
  47.         {   
  48.             if (Coll_Wrapper_BE == null)   
  49.                 Coll_Wrapper_BE = CLS_Multi_Dept_Checked_Wrapper.Get_Wrapper_Coll(new List<int>());   
  50.         }   
  51.   
  52.         public static readonly DependencyProperty Str_Department_IdsProperty = DependencyProperty.Register("Str_Department_Ids"typeof(string), typeof(UC_Multi_Dept_Combo), new PropertyMetadata(null, OnPropertyChangedCallback));   
  53.         public string Str_Department_Ids   
  54.         {   
  55.             get   
  56.             {   
  57.                 return (string)GetValue(Str_Department_IdsProperty);   
  58.             }   
  59.             set   
  60.             {   
  61.                 SetValue(Str_Department_IdsProperty, value);   
  62.             }   
  63.         }   
  64.   
  65.         private static void OnPropertyChangedCallback(DependencyObject o, DependencyPropertyChangedEventArgs e)   
  66.         {   
  67.             var obj = (UC_Multi_Dept_Combo)o;   
  68.               
  69.             obj.Coll_Wrapper_BE = CLS_Multi_Dept_Checked_Wrapper.Get_Wrapper_Coll(Globle_Function.Get_Coll_From_Str_CSV(obj.Str_Department_Ids));   
  70.         }   
  71.   
  72.         void item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)   
  73.         {   
  74.             if (e.PropertyName == "IS_CHECKED")   
  75.             {   
  76.                 SetString();   
  77.                 //Department_id_String();   
  78.             }   
  79.         }   
  80.   
  81.         public void SetString()   
  82.         {   
  83.             string str_Department_Name = "";   
  84.             string str_Department_Ids = "";   
  85.             foreach (var item in Coll_Wrapper_BE)   
  86.             {   
  87.                 if (item.IS_CHECKED)   
  88.                 {   
  89.                     if (str_Department_Name != "") str_Department_Name += ", ";   
  90.                     if (str_Department_Ids != "") str_Department_Ids += ", ";   
  91.                     str_Department_Name += item.DEPT_MST.DEPARTMENT_NAME;   
  92.                     str_Department_Ids += item.DEPT_MST.DEPARTMENT_ID.ToString();   
  93.                 }   
  94.             }   
  95.           
  96.             SetValue(Str_Department_IdsProperty, str_Department_Ids);   
  97.             tbDepartment.Text = str_Department_Name;   
  98.             var Tool = new ToolTip() { Content = str_Department_Name };   
  99.             ToolTipService.SetToolTip(tbDepartment, Tool);   
  100.             ToolTipService.SetToolTip(cmbDepartment, Tool);   
  101.         }   
  102.   
  103.         void cmbDepartment_SelectionChanged(object sender, SelectionChangedEventArgs e)   
  104.         {   
  105.             cmbDepartment.SelectedItem = null;   
  106.         }   
  107.   
  108.         private void tbDepartment_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)   
  109.         {   
  110.             cmbDepartment.IsDropDownOpen = true;   
  111.         }   
  112.     }   
  113. }  
XAML Code
  1. <UserControl x:Class="Banthia_Mgt_System.Assets.Multiselect_Combo.UC_Multi_Dept_Combo"   
  2.  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
  3.  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
  4.  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"   
  5.  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   
  6.  mc:Ignorable="d" d:DesignWidth="300" >   
  7.    
  8.  <Grid x:Name="LayoutRoot" Background="White">   
  9.          <ComboBox Name="cmbDepartment" Height="23" HorizontalAlignment="Stretch">   
  10.              <ComboBox.ItemTemplate>   
  11.                  <DataTemplate>   
  12.                      <CheckBox Content="{Binding DEPT_MST.DEPARTMENT_NAME}" IsChecked="{Binding IS_CHECKED,Mode=TwoWay}" />   
  13.                  </DataTemplate>   
  14.              </ComboBox.ItemTemplate>   
  15.          </ComboBox>   
  16.          <TextBlock x:Name="tbDepartment" TextTrimming="WordEllipsis" MouseLeftButtonDown="tbDepartment_MouseLeftButtonDown" HorizontalAlignment="Stretch" VerticalAlignment="Center" MinHeight="15" Text="" Margin="5,0,20,0" />   
  17.    
  18.      </Grid>   
  19.  </UserControl>  
Wrapper Class
  1. using System;   
  2. using System.Linq;   
  3. using System.Net;   
  4. using System.Windows;   
  5. using System.Windows.Controls;   
  6. using System.Windows.Documents;   
  7. using System.Windows.Ink;   
  8. using System.Windows.Input;   
  9. using System.Windows.Media;   
  10. using System.Windows.Media.Animation;   
  11. using System.Windows.Shapes;   
  12. using Banthia_Mgt_System.ServiceReference1;   
  13. using System.Collections.Generic;   
  14. using System.Collections.ObjectModel;   
  15. using Banthia_Mgt_System.Classes;   
  16. using System.ComponentModel;   
  17.   
  18. namespace Banthia_Mgt_System.Assets.Wrapper_Class   
  19. {   
  20.     public class CLS_Multi_Dept_Checked_WrapperBE : INotifyPropertyChanged   
  21.     {   
  22.         private bool _IS_CHECKED;   
  23.         public bool IS_CHECKED   
  24.         {   
  25.             get { return _IS_CHECKED; }   
  26.             set   
  27.             {   
  28.                 _IS_CHECKED = value;   
  29.                 OnPropertyChanged("IS_CHECKED");   
  30.             }   
  31.         }   
  32.   
  33.         public DEPARTMENT_MASTER DEPT_MST { getset; }   
  34.         #region INotifyPropertyChanged Members   
  35.         public event PropertyChangedEventHandler PropertyChanged;   
  36.         private void OnPropertyChanged(string propertyName)   
  37.         {   
  38.             if (this.PropertyChanged != null)   
  39.             {   
  40.                 this.PropertyChanged(thisnew PropertyChangedEventArgs(propertyName));   
  41.             }   
  42.         }   
  43.     #endregion   
  44.     }   
  45.   
  46.     public static class CLS_Multi_Dept_Checked_Wrapper   
  47.     {   
  48.         public static List<CLS_Multi_Dept_Checked_WrapperBE> Get_Wrapper_Coll(List<int> Coll_Dept_ID)   
  49.         {               
  50.             var CollBE = new List<CLS_Multi_Dept_Checked_WrapperBE>();   
  51.             foreach (var item in GlobalClass.GColl_DeptMaster)   
  52.             {   
  53.                 var obj = new CLS_Multi_Dept_Checked_WrapperBE()   
  54.                 {   
  55.                     IS_CHECKED = (Coll_Dept_ID.Where(ar => ar == item.DEPARTMENT_ID).Count() > 0),   
  56.                     DEPT_MST = item   
  57.                 };   
  58.                 CollBE.Add(obj);   
  59.             }   
  60.             return CollBE;   
  61.         }   
  62.         public static List<int> Get_Object_BE_Coll(List<CLS_Multi_Dept_Checked_WrapperBE> Coll)   
  63.         {   
  64.             List<int> NewColl = new List<int>();   
  65.             if (Coll != null)   
  66.                 NewColl = new List<int>(Coll.Where(ar => ar.IS_CHECKED == true).Select(ar => ar.DEPT_MST.DEPARTMENT_ID));   
  67.             return NewColl;   
  68.         }   
  69.     }   
  70. }