Version

Performing Selection Programmatically (xamComboEditor)

Topic Overview

Purpose

This topic describes how to programmatically perform a selection in the xamComboEditor™ control.

Required background

The following topic is a prerequisite to understanding this topic:

Topic Purpose

This topic describes how to get started with the xamComboEditor control and how to add it to your page using procedural code.

Programmatic Selection Summary

Programmatic selection summary chart

The following table briefly explains the configurable aspects of the programmatic selection in the xamComboEditor control and maps them to properties that configure them. Further details are available after the table.

Programmatic selection task Details Properties

While working with the XamComboEditor Items collection, configure a particular ComboEditorItem to be selected using the IsSelected property.

Configure the selected xamComboEditor item using the SelectedIndex property.

Configure the selected xamComboEditor data item using the SelectedItem property.

In case a multiple selection is enabled, the SelectedItem returns the last user selected item.

Configure the selected data items collection using the SelectedItems property.

Configure the Selected/Unselected data items using an underlying data model’s property which boolean value determines the items’ current state in the xamComboEditor .

Configure the value of the selected item using the SelectedValue property.

The path to the data item property used for selection is specified by the SelectedValuePath.

Configure a collection of selected items values using the SelectedValues property.

The path to the data item property used for selection is specified by the SelectedValuePath.

Configuring the Selected Combo Editor Item

Overview

Configure the selected xamComboEditor ComboEditorItem using the bool IsSelected property.

Property settings

The following table maps the desired configuration to the property settings that manage it.

In order to: Use this property: And set it to:

Configure the selected combo editor item

bool

Example

The screenshot below demonstrates how the xamComboEditor would behave as a result of the following settings:

Property Value

true

Selection 1.png

Following is the code that implements this example.

In XAML:

<ig:XamComboEditor x:Name="ComboEditor"
                   ItemsSource="{Binding Path=Products}"
                   DisplayMemberPath="ProductName"
                   Height="30" Width="200">
</ig:XamComboEditor>

In C#:

ComboEditor.Items[2].IsSelected = true;

In Visual Basic:

ComboEditor.Items(2).IsSelected = True

Configuring the Selected Combo Item through its Index

Overview

Configure the selected xamComboEditor item through its index and using the SelectedIndex property.

The default value of the SelectedIndex property is -1, and there is no selected item.

When multiple selection is enabled, the SelectedIndex returns the index of the last user selected combo item.

Property settings

The following table maps the desired configuration to the property settings that manage it.

In order to: Use this property: And set it to:

Configure the selected combo item using its index

int

Example

The screenshot below demonstrates how the xamComboEditor would behave as a result of the following settings:

Property Value

2

Selection 1.png

Following is the code that implements this example.

In XAML:

<ig:XamComboEditor x:Name="ComboEditor"
                   ItemsSource="{Binding Path=Products}"
                   DisplayMemberPath="ProductName"
                   SelectedIndex="2"
                   Height="30" Width="200">
</ig:XamComboEditor>

Configuring a Single Selected Data Item

Overview

Configure the selected data item using the xamComboEditor SelectedItem property. This item is a reference to the item within the used data model.

Selecting an item in the xamComboEditor triggers the SelectionChanged event. This event has an event argument SelectionChangedEventArgs which exposes the AddedItems/ RemovedItems In/From the selection.

If the xamComboEditor AllowMultipleSelection property is set to true:

• the SelectedItem returns the last user selected item

• setting the SelectedItem to a new data object clears the existing selected items collection

Note
Note

An exception is thrown if more than one item is set in runtime to the SelectedItem property when the AllowMultipleSelection property is set to false.

Property settings

The following table maps the desired configuration to the property settings that manage it.

In order to: Use this property: And set it to:

Configure the selected data item

object

Example

Following is the code that implements this example.

In XAML:

<ig:XamComboEditor x:Name="ComboEditor"
                   ItemsSource="{Binding Path=Products}"
                   DisplayMemberPath="ProductName"
                   Height="30" Width="200"
                   SelectionChanged="ComboEditor_OnSelectionChanged"/>

In C#:

private void ComboEditor_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (ComboEditor.SelectedItem != null)
    {
        var selectedItem = ComboEditor.SelectedItem as Product;
        System.Diagnostics.Debug.WriteLine(String.Format("Product info: {0} {1}", selectedItem.ProductID, selectedItem.ProductName));
    }
}

In Visual Basic:

Private Sub ComboEditor_OnSelectionChanged(sender As Object, e As SelectionChangedEventArgs)
    If ComboEditor.SelectedItem IsNot Nothing Then
        Dim selectedItem = TryCast(ComboEditor.SelectedItem, Product)
       System.Diagnostics.Debug.WriteLine([String].Format("Product info: {0} {1}", selectedItem.ProductID, selectedItem.ProductName))
      End If
End Sub

Configuring a Collection of Selected Data Items

Overview

Configure the selected data items collection using the xamComboEditor SelectedItems property. This property is extremely useful in case multiple selection is enabled in the control.

As this is a Two-Way bindable dependency property, additionally a collection of items can be set to the xamComboEditor . This collection has to be of type ObservableCollection<object>.

Note
Note

Multiple selection has to be enabled in the xamComboEditor control.

Property settings

The following table maps the desired configuration to the property settings that manage it.

In order to: Use this property: And set it to:

Set a collection of selected data items

ObservableCollection<object>

Modifying the Selected Data Items Collection via a Data Model Boolean Property’s Value

Overview

Configure the Selected/Unselected data items using an underlying data model’s property which boolean value determines the items’ current state in the xamComboEditor . The underlying data model’s property name is set to the IsSelectedMemberPath property.

Once the IsSelectedMemberPath is set to a data model property path; the SelectedItems collection is populated with the data items that have the specified property value set to true. If the underlying data model supports the INotifyPropertyChanged interface, modifications over the specified property values reflect over the xamComboEditor items’ selection state.

Note
Note

The data model’s property that determines the Selected/Unselected state of the data item in the xamComboEditor has to be of type bool.

Note
Note

If you enable the multiple selection and specify an IsSelectedMemberPath; the AllowMultipleSelection property should be defined in XAML first.

Note
Note

Using the IsSelectedMemberPath property has an impact over the performance of the xamComboEditor control depending on the size of the data source bound to it.

Property settings

The following table maps the desired configuration to the property settings that manage it.

In order to: Use this property: And set it to:

Configure items selection depending on the underlying data model’s property boolean value

string

Example

The screenshot below demonstrates how the xamComboEditor looks as a result of the following settings:

Property Value

IsAvailable

Selection 3.png

Following is the code that implements this example.

In XAML:

<Grid>
    <Grid.DataContext>
        <local:DataProvider />
    </Grid.DataContext>
    <ig:XamComboEditor x:Name="DataCombo" Height="30" Width="250"
                       ItemsSource="{Binding Path=ProductItems}"
                       CheckBoxVisibility="Visible"
                       AllowMultipleSelection="True"
                       IsSelectedMemberPath="IsAvailable">
        <ig:XamComboEditor.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding Path=ProductName}" />
                    <StackPanel Orientation="Horizontal"
                                Margin="10,0,0,0"
                                Grid.Column="1">
                        <TextBlock Text="is available" />
                            <TextBlock Text="{Binding Path=IsAvailable}"
                                       Margin="10,0,0,0"
                                       FontWeight="Bold"/>
                    </StackPanel>
                </Grid>
            </DataTemplate>
        </ig:XamComboEditor.ItemTemplate>
    </ig:XamComboEditor>
</Grid>

The following class is the data model used in the example:

In C#:

public class ProductItem : INotifyPropertyChanged
    {
        public ProductItem(string name, bool isAvailable)
        {
            _productName = name;
            _isAvailable = isAvailable;
        }
        private string _productName;
        public string ProductName
        {
            get { return this._productName; }
            set
            {
                if (this._productName != value)
                {
                    this._productName = value;
                    this.OnPropertyChanged("ProductName");
                }
            }
        }
        private bool _isAvailable;
        public bool IsAvailable
        {
            get { return this._isAvailable; }
            set
            {
                if (this._isAvailable != value)
                {
                    this._isAvailable = value;
                    this.OnPropertyChanged("IsAvailable");
                }
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
                handler(sender, e);
        }
        protected void OnPropertyChanged(string propertyName)
        {
            OnPropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

In C#:

public class DataProvider : INotifyPropertyChanged
    {
        public DataProvider()
        {
            DownloadDataSource();
        }
        private ObservableCollection<ProductItem> _productItems = null;
        public ObservableCollection<ProductItem> ProductItems
        {
            get { return this._productItems; }
            set
            {
                if (this._productItems != value)
                {
                    this._productItems = value;
                    this.OnPropertyChanged("ProductItems");
                }
            }
        }
        private void DownloadDataSource()
        {
            var data = new ObservableCollection<ProductItem>();
            data.Add(new ProductItem("product item 1", true));
            data.Add(new ProductItem("product item 2", false));
            data.Add(new ProductItem("product item 3", true));
            data.Add(new ProductItem("product item 4", true));
            data.Add(new ProductItem("product item 5", false));
            data.Add(new ProductItem("product item 6", true));
            this._productItems = data;
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
                handler(sender, e);
        }
        protected void OnPropertyChanged(string propertyName)
        {
            OnPropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

Configuring a Single Selected Value

Overview

Configure the value of the selected item using the xamComboEditor SelectedValue property along with the SelectedValuePath to specify the path to the SelectedItem member used for selection.

If there are several values in the data items collection that are equal to the specified SelectedValue, the item to be selected is the first one discovered.

Note
Note

The SelectedValuePath property needs to be set prior to the setting of the SelectedValue property.

Property settings

The following table maps the desired configuration to the property settings that manage it.

In order to: Use this property: And set it to:

Configure the path to the data item property used for selection

string

Configure the selected data item property’s value

object

Example

Assume that the xamComboEditor contains a collection of data items of type Person and this data type has two properties – ID and Name. The xamComboEditor can display the people’s names and selection to operate on people’s ids.

The screenshot below demonstrates how the xamComboEditor control behaves as a result of the following settings:

Selection 4.png

Following is the code that implements this example.

In XAML:

<ig:XamComboEditor x:Name="ComboEditor"
                   ItemsSource="{Binding Path=People}"
                   SelectedValuePath="ID"
                   SelectedValue="4"
                   DisplayMemberPath="Name"
                   Height="30" Width="200"/>

Configuring a Collection of Selected Values

Overview

Configure a collection of selected items values using the SelectedValues property. It is extremely useful in case multiple selection is enabled in the control.

The path to the data item property used for selection is specified by the SelectedValuePath.

Note
Note

The data item property specified by the SelectedValuePath is expected to hold unique values. If there are several values in the data items collection that are equal to any of the specified SelectedValues, only the first one discovered will be selected.

Property settings

The following table maps the desired configuration to the property settings that manage it.

In order to: Use this property: And set it to:

Configure the path to the data item property used for selection

string

Configure the selected data item property’s value

object[]

Example

The screenshot below demonstrates how the xamComboEditor looks as a result of the following settings:

Property Value

new object[] { 1, 2, 3 }

True

Selection 5.png

Following is the code that implements this example.

In XAML:

<ig:XamComboEditor x:Name="ComboEditor"
                   ItemsSource="{Binding Path=Categories}"
                   AllowMultipleSelection="True"
                   SelectedValuePath="CategoryID"
                   Height="30" Width="200">
    <ig:XamComboEditor.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding CategoryID}" Margin="0,0,5,0"/>
                    <TextBlock Text="{Binding CategoryName}" />
            </StackPanel>
        </DataTemplate>
    </ig:XamComboEditor.ItemTemplate>
</ig:XamComboEditor>

In C#:

ComboEditor.SelectedValues = new object[] {1, 2, 3};

In Visual Basic:

ComboEditor.SelectedValues = New Object() {1, 2, 3}

Related Topics

The following topics provide additional information related to this topic.

Topic Purpose

This topic describes user selection interactions in the xamComboEditor control.

This topic describes how to configure multiple selection in the xamComboEditor control.