Oct 16, 2005

What is the difference between SelectedValue and SelectedItem?

When they are used by themselves, these two properties are very similar. The need for both and the difference between the two becomes apparent when SelectedValuePath is also set.

For example, consider our well-known GreekGods data source. I set the DataContext of the StackPanel to be that collection through code:

    GreekGods items;
    items = new GreekGods();
    mainStackPanel.DataContext = items;

And used an empty Binding to bind that collection to the ListBox. I know that I want to select the GreekGod with description “Messenger of the Gods” (even though I am only displaying the Name of each God). This is when SelectedValuePath becomes useful. Each item in the ListBox is a GreekGod object, so by setting SelectedValuePath to “Description” I am able to drill down into the Description property of each GreekGod. Then I just have to set SelectedValue to the description I am looking for and the item becomes selected.

    <StackPanel Name="mainStackPanel">
        <ListBox ItemsSource="{Binding}" DisplayMemberPath="Name" SelectedValue="Messenger of the Gods" SelectedValuePath="Description" Name="listBox1" (…) />

The difference between SelectedValue and SelectedItem should be obvious now. SelectedValue returns the string it was set to (“Messenger of the Gods”), while SelectedItem returns the actual GreekGod object with that description.

    string messengerOfGods = (string)(listBox1.SelectedValue);
    GreekGod hermes = (GreekGod)(listBox1.SelectedItem);

SelectedValue is particularly useful when only part of your item is stored in the model you are data binding to. In this scenario, you would data bind the SelectedValue property to the partial information in your model but the ListBox can show a lot more information about that item.

If you have ideas of how to combine these two properties in one, we would love to hear it.

Here you can find the VS project with this sample code. Put a breakpoint in the handler for the button click to inspect the values of SelectedValue and SelectedItem when the button is clicked. This works with September CTP WPF bits.

  1. Ifeanyi [Fancy Pants] Echeruo

    I wish somehow I could databind to xml without having to marshal the xml into clr objects. Now that would be some fancy data binding. Can your fancy pants Avalon do *THAT* hmmm?

    • Bea

      I will take that as a request to show XmlDataProvider in my next sample.

  2. Sam

    Nice, SelectedValuePath does work just the same in the ListView.

    One question, though: how do I select more than one item, or deselect one, from the programs point of view?

    • Bea

      Hi Sam,

      It is not possible to use SelectedValuePath to select more than one item. If you want to allow multiple selection in a ListBox, you should set its SelectionMode property to Multiple first. Then you can simply add data items to the collection in the SelectedItems property. Similarly, to deselect an item, you can simply remove it from that collection.

      I made a sample that shows this. You can find it here.

  3. Ken

    THANK YOU BEA! I’ve been struggling getting binding to work with a custom combobox & CollectionViews but was getting no where with SelectedItem binding even when defining my own equality and hash for the POCO I was binding to. I didn’t know SelectedValue was an option for TwoWay binding but with your help here it works like a champ! I just wish the happy path in WPF wasn’t so narrow. Thanks again for your great blog!

Comments are closed.