ComponentOne Input Library for WPF
Input Library Overview / Multiselect / 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. In XAML Design, drag and drop the C1MultiSelect control from the toolbox onto the window.
    2. Create a class named Customer using the following code:
      C#
      Copy Code
      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;
              }
          }
      }
      
    3. Add objects of Customer class to a BindingList and set C1MultiSelect’s ItemsSource and DisplayMemberPath properties using the following code:
      C#
      Copy Code
      public BindingList<Customer> customers;
      
      public ObjectCollectionBinding()
      {
          InitializeComponent();
          Loaded += OnLoaded;
      }
      
      private void OnLoaded(object sender, RoutedEventArgs e)
      {
          customers = new BindingList<Customer>(); // initializing binding source
          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"));
          multiSelect.ItemsSource = customers; // setting items source
          multiSelect.DisplayMemberPath = "Name"; // setting display member path
      }