Difference between SelectedItem, SelectedValue and SelectedValuePath
Asked Answered
E

5

414

What is the difference betweeen the following:

All these dependency properties are defined in Selector class. I often confuse SelectedItem with SelectedValue , and SelectedValue with SelectedValuePath.

I would like to know the difference between them, and also when do we use them, especially SelectedValue and SelectedValuePath. Please explain their use with some simple examples.

Eyot answered 4/2, 2011 at 19:22 Comment(1)
possible duplicate of [Confused with wpf ComboBox DisplayMemberPath,SelectedValue and SelectedValuePath ](#3797534)Imperil
U
633

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>. The SelectedItem property will return you the currently selected Category 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 its ID 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 a CategoryID 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 the CategoryID property on the Product object. This is where the SelectedValuePath and the SelectedValue 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 using SelectedValuePath='ID', and then bind the SelectedValue 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

Untouchability answered 4/2, 2011 at 20:10 Comment(7)
Thanks for the explanation. Are you the same guy who have written book on Silverlight? I don't believe I'm talking to you. :-)... I'm reading this book nowadays. Really an awesome work. :-)Eyot
Yep, that's me :). I hang around these here parts to answer questions when I can. Glad you're enjoying the book :). Since you've got the book, you'll find more information on this topic on pages 69-70, and page 204.Untouchability
+1. +100. You, sir, are a golden god amongst lesser men. May God bless you, and all who sail in you. :) Seriously, I just spent two hours trying to figure that out - thank you!Apelles
I appreciate the effort put into this answer, but to be honest I was almost as confused after reading it as I was before. IMO, this other SO answer is much clearer.Penetrant
@StevenRands Thank you for sharing the link to the other response. I also agree with you. For me, this response is better, more concise and easier to understand than the response from @Chris. I said for me since we all have different ways of understanding things. Maybe, Chriss response is better to understand for some other readers - and for that reason, I've upvoted Chriss response, as well.Phalanger
One think I'd like to point out - When working with value types (string, int, ...), SelectedValue seems to work better than SelectedItem. You'll run into bugs if the SelectedItem is of the same value as an option in the ComboBox, but instantiated elsewhere (i.e. different reference), like in a converter; no option will be picked.Hepzi
Not sure why, but I ended up having to bind both SelectedItem and SelectedValue to get by combobox binding properly both ways (UI to ViewModel and ViewModel to UI).Leyes
S
81

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.

Study answered 4/2, 2011 at 19:53 Comment(0)
P
12

SelectedItem and SelectedValue are an object. and SelectedValuePath is a string.

for example using the ListBox:

Below listbox1.SelectedValue becomes a string value.

string value = listbox1.SelectedValue;

if you say give me listbox1.SelectedItem it will give you the entire object.

ListItem item = listbox1.SelectedItem;
string value = item.value;
Potoroo answered 4/2, 2011 at 19:26 Comment(5)
SelectedValue is also an object and not a string. Pls see hereOvermatch
Sorry man, I hate giving down votes, but SelectedValue is not a string, unless of course the collection is a collection of strings. This really needs to be corrected in case someone erroneously believes this.Castanon
@Castanon I think I corrected.Potoroo
@capdragon, thanks, down-vote retracted.Castanon
Wanting to add that ListItem.Value does not exist in the scope of WPF and doesn't work for my strictly WPF desktop application (i.e., it's not a web app). That's a part of System.Web.UI.WebControls according to the documentation.Militarize
O
5

inspired by this question I have written a blog along with the code snippet here. Below are some of the excerpts from the blog

SelectedItem – Selected Item helps to bind the actual value from the DataSource which will be displayed. This is of type object and we can bind any type derived from object type with this property. Since we will be using the MVVM binding for our combo boxes in that case this is the property which we can use to notify VM that item has been selected.

SelectedValue and SelectedValuePath – These are the two most confusing and misinterpreted properties for combobox. But these properties come to rescue when we want to bind our combobox with the value from already created object. Please check my last scenario in the following list to get a brief idea about the properties.

Obstruent answered 3/8, 2015 at 6:31 Comment(0)
C
1

Every control that uses Collections to store data have SelectedValue, SelectedItem property. Examples of these controls are ListBox, Dropdown, RadioButtonList, CheckBoxList.

To be more specific if you literally want to retrieve Text of Selected Item then you can write:

ListBox1.SelectedItem.Text;

Your ListBox1 can also return Text using SelectedValue property if value has set to that before. But above is more effective way to get text.

Now, the value is something that is not visible to user but it is used mostly to store in database. We don't insert Text of ListBox1, however we can insert it also, but we used to insert value of selected item. To get value we can use

ListBox1.SelectedValue

Source

Codicil answered 13/3, 2014 at 19:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.