Telerik UI for Windows Phone by Progress

The purpose of this tutorial is to show you the basic properties exposed by the RadListPicker for working with selection. This topic includes the following sections:

Setting SelectionMode

The RadListPicker provides three selection modes, which allow you to manipulate the type of selection. This is controlled by the SelectionMode enumeration property which has the following entries:

  • Single - only one item can be selected at a time. (default value)
  • Multiple - items are added to the selection when they get clicked and get removed when they get clicked again.
  • Extended - in the context of Windows Phone, Extended selection is the same as Multiple selection.

Using the SelectedItem

The purpose of the SelectedItem property is to get or set the currently selected item of the RadListPicker. Bellow is one possible way of accessing the SelectedItem of RadListPicker.

  • When your RadListPicker is data bound to a collection of custom objects, the SelectedItem is of the type of the custom object.

    CopyC#
    var selectedItem = this.listPicker1.SelectedItem as Customer;

Using the SelectedItems

The purpose of the SelectedItems property is to allow you obtain all selected items in RadListPicker. The only requirement is to set the SelectionMode property to Multiple or Extended.

  • When your RadListPicker is data bound to a collection of custom objects, you can obtain the selected items by iterating over the SelectedItems property of RadListPicker.

    CopyC#
    foreach (var item in this.listPicker1.SelectedItems)
    {
        Customer customer = item as Customer;
        if (customer != null)
        {
            // access the customer object
        }
    }

Using the SelectedIndex

Use the SelectedIndex property to get or set the index of the selected item. For example, by using the SelectedIndex property, you could specify which the default selected item is.

CopyXAML
<telerikInput:RadListPicker x:Name="listPicker1" SelectedIndex="2" />

Using the SummaryForSelectedItems delegate

The purpose of the SummaryForSelectedItemsDelegate is to provide you with an entry point that allows you to modify the SelectedItems representation after closing the popup. The most common case is when you want to modify the separator between the selected items. The example bellow demonstrates how to put a "," between the selected items.

CopyC#
this.listPicker1.SummaryForSelectedItemsDelegate = (IList list) =>
{
    string summary = String.Empty;

    for (int i = 0; i < list.Count; i++)
    {
        // check if the last item has been reached so we don't put a "," at the end
        bool isLast = i == list.Count - 1;

        Customer item = (Customer)list[i];
        summary = String.Concat(summary, item.Name);
        summary += isLast ? string.Empty : ", ";
    }
    if (summary == String.Empty)
    {
        summary = "none";
    }
    return summary;
};

The SummaryForSelectedItemsDelegate can be used for custom "grouping" as well. Imagine the scenario where the user is prompted to select one or more week days. If the user selects Saturday and Sunday, RadListPicker could show "weekends" instead of "Saturday, Sunday". The same goes if the user selects Monday through Friday. The RadListPicker can be customized to display "weekdays" instead of "Monday, Tuesday, Wednesday, Thursday, Friday".

CopyC#
this.listPicker1.SummaryForSelectedItemsDelegate = (IList list) =>
{
    string summary = String.Empty;
    if (list.Count == 0)
    {
        summary = "none";
    }
    else if (list.Count == 2 && list.Contains(DayOfWeek.Saturday) && list.Contains(DayOfWeek.Sunday))
    {
        summary = "weekends";
    }
    else if (list.Count == 5 && !list.Contains(DayOfWeek.Saturday) && !list.Contains(DayOfWeek.Sunday))
    {
        summary = "weekdays";
    }
    else
    {
        for (int i = 0; i < list.Count; i++)
        {
            // check if the last item has been reached so we don't put a "," at the end
            bool isLast = i == list.Count - 1;

            DayOfWeek item = (DayOfWeek)list[i];
            summary = String.Concat(summary, item.ToString().ToLower());
            summary += isLast ? string.Empty : ", ";
        }
    }
    return summary;
};