# Selection

The purpose of this tutorial is to show you the basic properties exposed by the **RadListBox** for working with selection.

## 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**

```C#
var selectedItem = radListBox.SelectedItem as RadListBoxItem;
```

* When your **RadListBox** is [data bound to a collection of custom objects](https://www.telerik.com/products/wpf/documentation/controls/radlistbox/populating-with-data/binding-to-object), the SelectedItem is of the type of the custom object.

**Getting the SelectedItem of type custom object**

```C#
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](https://www.telerik.com/products/wpf/documentation/controls/radlistbox/populating-with-data/binding-to-object) to a list of Customer objects.

**Business object named Customer**

```C#
public class Customer
{
    public string Name { get; set; }
    public string City { get; set; }
}
```

**Initializing of RadListBox**

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

![radlistbox populatingwithdata bindingtoobject 020](https://www.telerik.com/products/wpf/documentation/assets/df8ed4bccd76cd76700e9613e30ee214/radlistbox_populatingwithdata_bindingtoobject_020.png)

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**

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

## Using the SelectedItems Collection

The selection is also saved in the `SelectedItems` collection of `RadListBox`. The collection stores multiple items when multiple selection is enabled. It can be used to access or add/remove items from the selection.

```C#
IList selection = this.radListBox.SelectedItems;
```

The `SelectedItems` property cannot be set, thus it cannot be data bound. In case the selection should be synced with the view model, the [ListBoxSelectedItemsBehavior.SelectedItemsSource](https://www.telerik.com/products/wpf/documentation/controls/radlistbox/features/selecteditemssource) attached property can be used.

## 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

* [Getting Started](https://www.telerik.com/products/wpf/documentation/controls/radlistbox/getting-started)

* [Populating with Data](https://www.telerik.com/products/wpf/documentation/controls/radlistbox/populating-with-data/overview)
