MultiSelect for WPF | ComponentOne
Data Binding / Bind MultiSelect to Object Collection
In This Topic
    Bind MultiSelect to Object Collection
    In This Topic

    Bind MultiSelect for WPF control to a list of complex data objects which can have multiple properties. To bind the control to a list of data objects, follow these steps:

    1. Create a class named Customer using the following code.
      Public Class Customer
      
          Private name As String
      
          Private customerID As String
      
          Private mobile As String
      
          Private email As String
      
          Public Sub New(ByVal _name As String, ByVal _custId As String,
                         ByVal _mobile As String, ByVal _email As String)
              Me.name = _name
              Me.customerID = _custId
              Me.mobile = _mobile
              Me.email = _email
          End Sub
      
          Public Property Name As String
              Get
                  Return name
              End Get
          End Property
      
          Public Property CustomerID As String
              Get
                  Return customerID
              End Get
          End Property
      
          Public Property Mobile As String
              Get
                  Return mobile
              End Get
          End Property
      
          Public Property Email As String
              Get
                  Return email
              End Get
          End Property
      End Class
      
      public class Customer
      {
          string name;
          string customerID;
          string mobile;
          string email;
          public Customer(string _name, string _custId, string _mobile, string _email)
          {
              this.name = _name;
              this.customerID = _custId;
              this.mobile = _mobile;
              this.email = _email;
          }
      
          public string Name
          {
              get
              {
                  return name;
              }
          }
          public string CustomerID
          {
              get
              {
                  return customerID;
              }
          }
          public string Mobile
          {
              get
              {
                  return mobile;
              }
          }
          public string Email
          {
              get
              {
                  return email;
              }
          }
      }
      
    2. Add objects of Customer class to a BindingList and set C1MultiSelect’s ItemsSource and DisplayMemberPath properties at Window_Loaded event.
      Public customers As BindingList(Of Customer)
      
      Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
          customers = New BindingList(Of Customer)()
          customers.Add(New Customer("John", "C001", "8888832141", "john@gmail.com"))
          customers.Add(New Customer("Alex", "C002", "8888832142", "@gmail.com"))
          customers.Add(New Customer("Shawn", "C003", "8888832143", "shawn@gmail.com"))
          customers.Add(New Customer("Sam", "C004", "8888832144", "sam@gmail.com"))
          customers.Add(New Customer("Neo", "C005", "8888832145", "neo@gmail.com"))
          customers.Add(New Customer("Paul", "C006", "8888832146", "paul@gmail.com"))
          Me.mselect.DataContext = customers
      End Sub
      
      public BindingList<Customer> customers;
      private void Window_Loaded(object sender, RoutedEventArgs e)
      {
           customers = new BindingList<Customer>();
           customers.Add(new Customer("John", "C001", "8888832141", "john@gmail.com"));
           customers.Add(new Customer("Alex", "C002", "8888832142", "@gmail.com"));
           customers.Add(new Customer("Shawn", "C003", "8888832143", "shawn@gmail.com"));
           customers.Add(new Customer("Sam", "C004", "8888832144", "sam@gmail.com"));
           customers.Add(new Customer("Neo", "C005", "8888832145", "neo@gmail.com"));
           customers.Add(new Customer("Paul", "C006", "8888832146", "paul@gmail.com"));
           
           this.mselect.DataContext = customers;       
      }