Difference between SelectedItem, SelectedValue and SelectedValuePath
Solution 1:
Their names can be a bit confusing :). Here's a summary:
The SelectedItem property returns the entire object that your list is bound to. So say you've bound a list to a collection of
Category
objects (with each Category object having Name and ID properties). eg.ObservableCollection<Category>
. TheSelectedItem
property will return you the currently selectedCategory
object. For binding purposes however, this is not always what you want, as this only enables you to bind an entire Category object to the property that the list is bound to, not the value of a single property on that Category object (such as itsID
property).Therefore we have the SelectedValuePath property and the SelectedValue property as an alternative means of binding (you use them in conjunction with one another). Let's say you have a
Product
object, that your view is bound to (with properties for things like ProductName, Weight, etc). Let's also say you have aCategoryID
property on that Product object, and you want the user to be able to select a category for the product from a list of categories. You need the ID property of the Category object to be assigned to theCategoryID
property on the Product object. This is where theSelectedValuePath
and theSelectedValue
properties come in. You specify that the ID property on the Category object should be assigned to the property on the Product object that the list is bound to usingSelectedValuePath='ID'
, and then bind theSelectedValue
property to the property on the DataContext (ie. the Product).
The example below demonstrates this. We have a ComboBox bound to a list of Categories (via ItemsSource). We're binding the CategoryID property on the Product as the selected value (using the SelectedValue property). We're relating this to the Category's ID property via the SelectedValuePath property. And we're saying only display the Name property in the ComboBox, with the DisplayMemberPath property).
<ComboBox ItemsSource="{Binding Categories}"
SelectedValue="{Binding CategoryID, Mode=TwoWay}"
SelectedValuePath="ID"
DisplayMemberPath="Name" />
public class Category
{
public int ID { get; set; }
public string Name { get; set; }
}
public class Product
{
public int CategoryID { get; set; }
}
It's a little confusing initially, but hopefully this makes it a bit clearer... :)
Chris
Solution 2:
To answer a little more conceptually:
SelectedValuePath
defines which property (by its name) of the objects bound to the ListBox's ItemsSource
will be used as the item's SelectedValue
.
For example, if your ListBox is bound to a collection of Person
objects, each of which has Name
, Age
, and Gender
properties, SelectedValuePath=Name
will cause the value of the selected Person
's Name
property to be returned in SelectedValue
.
Note that if you override the ListBox's ControlTemplate (or apply a Style) that specifies what property should display, SelectedValuePath
cannot be used.
SelectedItem
, meanwhile, returns the entire Person
object currently selected.
(Here's a further example from MSDN, using TreeView)
Update: As @Joe pointed out, the DisplayMemberPath property is unrelated to the Selected* properties. Its proper description follows:
Note that these values are distinct from DisplayMemberPath
(which is defined on ItemsControl, not Selector), but that property has similar behavior to SelectedValuePath
: in the absence of a style/template, it identifies which property of the object bound to item should be used as its string representation.