Intersoft ClientUI Documentation
How-to: Initialize Selected Items In UXMultipleSelectionComboBox

This example shows how to initialize selected items in UXMultipleSelectionComboBox.

Example

Description

Initialize selected items is one of the must-have feature in UXMultipleSelectionComboBox. It allows you to set the initial selected items in form editing scenario. With MVVM support, you can easily bind a collection property in ViewModel to the control's SelectedItems property using two-way binding mode. To learn more about UXMultipleSelectionComboBox, see UXMultipleSelectionComboBox Overview.

Code

XAML
Copy Code
<Intersoft:UXMultipleSelectionComboBox SearchResult="{Binding Customers}"
        FilterDescriptors="{Binding LookUpDescriptor.FilterDescriptors, Mode=TwoWay}" 
        SortDescriptors="{Binding LookUpDescriptor.SortDescriptors, Mode=TwoWay}" 
        PageDescriptor="{Binding LookUpDescriptor.PageDescriptor}" 
        DisplayMemberPath="ContactName" 
        SelectedItems="{Binding SelectedItems, Mode=TwoWay}"
        SelectedValuePath="CustomerID"> 
        <Intersoft:UXMultipleSelectionComboBox.Columns> 
                <Intersoft:UXDataComboBoxTextColumn Header="Customer ID" Binding="{Binding CustomerID}" DisplayMode="Image" ImageHeight="64" ImageWidth="64" 
                        ImageStretch="Fill" ImageBinding="{Binding PhotoPath}"/> 
                <Intersoft:UXDataComboBoxTextColumn Header="Contact Name" Binding="{Binding ContactName}"/>
                <Intersoft:UXDataComboBoxTextColumn Header="Company Name" Binding="{Binding CompanyName}"/>
                <Intersoft:UXDataComboBoxTextColumn Header="Country" Binding="{Binding Country}"/> 
        </Intersoft:UXMultipleSelectionComboBox.Columns> 

        <Intersoft:UXMultipleSelectionComboBox.DataContext> 
                <ViewModels:CustomerViewModel/> 
        </Intersoft:UXMultipleSelectionComboBox.DataContext> 
</Intersoft:UXMultipleSelectionComboBox> 
C#
Copy Code
using System;
using System.Collections;
using Intersoft.Client.Data.ComponentModel;
using Intersoft.ClientUI.Samples.DataControls.ModelServices;

namespace Intersoft.ClientUI.Samples.DataControls.ViewModels
{
        public class CustomerViewModel : ViewModelBase
        {
                #region Constructors

                public CustomerViewModel()
                {
                        this.CustomersSource = CustomersRepository.Instance;
                        this.Presenter = new MessagePresenter();
                        this.LookUpDescriptor = new QueryDescriptor();
                        
                        // Initialize Sort Descriptor
                        this.LookUpDescriptor.SuspendQueryChanged = true;
                        this.LookUpDescriptor.SortDescriptors.Add(new SortDescriptor() { PropertyName = "ContactName", Direction = ListSortDirection.Ascending });
                        this.LookUpDescriptor.SuspendQueryChanged = false;
                        
                        this.InitializeSelectedItems();
                }
                
                #endregion
                
                #region Fields

                private IEnumerable _customers;
                private QueryDescriptor _lookUpDescriptor;
                private INotifyCollectionChanged _selectedItems;
                
                #endregion
                
                #region Properties

                protected IDataRepository CustomersSource { get; set; }

                public IEnumerable Customers
                {
                        get { return this._customers; }
                        set
                        {
                                if (_customers != value)
                                {
                                        _customers = value;
                                        OnPropertyChanged("Customers");
                                }
                        }
                }

                public QueryDescriptor LookUpDescriptor
                {
                        get { return _lookUpDescriptor; }
                        set
                        {
                                if (_lookUpDescriptor != value)
                                {
                                        if (_lookUpDescriptor != null)
                                                _lookUpDescriptor.QueryChanged -= new EventHandler(OnLookUpQueryChanged);

                                        _lookUpDescriptor = value;
                                        _lookUpDescriptor.QueryChanged += new EventHandler(OnLookUpQueryChanged);

                                        OnPropertyChanged("LookUpDescriptor");
                                }
                        }
                }
                
                public INotifyCollectionChanged SelectedItems
                {
                        get { return _selectedItems; }
                        set
                        {
                                if (_selectedItems != value)
                                {
                                        _selectedItems = value;
                                        OnPropertyChanged("SelectedItems");
                                }
                        }
                }

                protected virtual MessagePresenter Presenter { get; private set; }

                #endregion
                
                #region Methods
                
                private void OnLookUpQueryChanged(object sender, EventArgs e)
                {
                        this.CustomersSource.GetData
                        (
                                this.LookUpDescriptor,
                                (customers) =>
                                {
                                        if (this.LookUpDescriptor.PageDescriptor.PageIndex > 0)
                                        {
                                                ObservableCollection<object> items = this.Customers as ObservableCollection<object>;
                                                foreach (var customer in customers)
                                                {
                                                        items.Add(customer);
                                                }
                                        }
                                        else
                                        {
                                                ObservableCollection<object> items = new ObservableCollection<object>(customers.Cast<object>());
                                                this.Customers = items;
                                        }
                                },
                                (totalItemCount) =>
                                {
                                        if (totalItemCount != -1)
                                                this.LookUpDescriptor.PageDescriptor.TotalItemCount = totalItemCount;
                                },
                                (error) =>
                                {
                                        this.Presenter.ShowErrorMessage(
                                                "An exception has occurred during data loading\n." +
                                                "Message: " + error.Message +
                                                "Stack Trace: " + error.StackTrace);
                                }
                        );
                }
                
                private void InitializeSelectedItems()
                {
                        // Set first 5 items from data source as initial selected items
                
                        QueryDescriptor queryDescriptor = new QueryDescriptor();
                        
                        queryDescriptor.SortDescriptors.Add(new SortDescriptor("ContactName", ListSortDirection.Ascending));
                        
                        queryDescriptor.PageDescriptor.PageIndex = 0;
                        queryDescriptor.PageDescriptor.PageSize = 5;

                        this.CustomersSource.GetData
                        (
                                queryDescriptor,
                                (customers) =>
                                {
                                        ObservableCollection<Customer> collection = new ObservableCollection<Customer>();
                                        foreach (Customer customer in customers)
                                        {
                                                collection.Add(new Customer()
                                                {
                                                        CustomerID = customer.CustomerID,
                                                        ContactName = customer.ContactName,
                                                        CompanyName = customer.CompanyName,
                                                        Country = customer.Country,
                                                        PhotoPath = customer.PhotoPath                                                          
                                                });
                                        }
                                        this.SelectedItems = collection;
                                },
                                (totalItemCount) =>
                                {

                                },
                                (error) =>
                                {
                                        MessageBox.Show(error.Message);
                                }
                        );
                }
                
                #endregion
        }
}

Results

The results produced by the code explained above will look like the following image:

See Also

Concepts