Selection

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

Setting SelectionMode

The RadListBox 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 - items are added to the selection only by combining the mouse clicks with the Ctrl or Shift key.

Using the SelectedItem

The purpose of the SelectedItem property is to get or set the currently selected item of the RadListBox. There are two common cases when accessing the SelectedItem property run-time.

  • When your RadListBox is with static data (declared in XAML), the SelectedItem property is of type RadListBoxItem.

Getting the SelectedItem of type RadListBoxItem

var selectedItem = radListBox.SelectedItem as RadListBoxItem; 

Getting the SelectedItem of type custom object

var customer = radListBox.SelectedItem as Customer; 

Using SelectedValue and SelectedValuePath/SelectedValueBinding

The SelectedValue property is used when you have linked the RadListBox to a data source, and you want to return a value other than the one which is displayed. The SelectedValuePath / SelectedValueBinding properties provide a way to specify a SelectedValue for the SelectedItem in a RadListBox. There are two essential things that you should remember here:

  • The SelectedItem property represents an object in the Items collection and the listbox displays the value of a single property of the selected item.

  • The SelectedValuePath / SelecteValueBinding properties specify the path to the property that is used to determine the value of the SelectedValue property.

If SelectedValuePath / SelecteValueBinding are not specified, SelectedValue should be equal to SelectedItem.

The following example demonstrates the usage of the SelectedItem, SelectedValue and SelectedValuePath properties.

Imagine that you have a business object named Customer with two members(properties): Name and City. And a RadListBox control which is data bound to a list of Customer objects.

Business object named Customer

public class Customer 
{ 
    public string Name { get; set; } 
    public string City { get; set; } 
} 

Initializing of RadListBox

<telerik:RadListBox  Width="300" x:Name="radListBox1"  
        ItemsSource="{Binding Customers, Source={StaticResource CustomerViewModel}}" 
        SelectedValuePath="City"                 
        DisplayMemberPath="Name"/> 

radlistbox populatingwithdata bindingtoobject 020

When you select a Customer name from the listbox, the SelectedItem property returns the Customer data item that corresponds to the selected Name. However, because the SelectedValuePath of this RadListBox is set to City, the SelectedValue is set to the City property of the Customer business 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.

Setting SelectedIndex

<telerik:RadListBox x:Name="radListBox" Width="200" SelectedIndex="3" /> 

Using the Text and TextPath / TextBinding

Use the RadListBox's Text property whenever you want to get the string representation of the currently selected item.

The TextPath / TextBinding properties specify the path to the property that is used to determine the value of the Text property.

Disabling Caching of Selected Items

By default, when its ItemsSource is reset, RadListBox will try to restore any previously selected items through its built-in caching mechanism. As of R1 2017, RadListBox allows you to disable this functionality by setting its new EnableSelectionCaching property to False.

See Also

In this article