views:

24

answers:

1

I have encountered something very strange, simple WPF application

<Window x:Class="ListBoxSelection.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox ItemsSource="{Binding Path=Strings}" SelectionMode="Single"/>
    </Grid>
</Window>

with code behind

public class ViewModel
{
    public List<string> Strings { get; set; }

    public ViewModel ()
    {
        Strings = new List<string> ();
        Strings.Add ("A");
        // add many items ...
        Strings.Add ("A");
    }
}

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow ()
    {
        InitializeComponent ();

        DataContext = new ViewModel ();
    }
}

and when I click on a single item,

Why multiple values selected?

if I continue clicking items, they just aggregate. Clicking an already selected item does nothing. Scratching my head, I have databound lists to ListBoxes before, and have never seen this before. Running Win7 (64), VS2010, behaviour presents with .Net 3.5, .Net 3.5 Client Profile, .Net 4, and .Net 4 Client Profile.

Arg, I should mention I am expecting normal, default, single-select behaviour.

+2  A: 

Dan Bryant got most of the answer in his comment.

What's going on here is string interning. When you create a bunch of strings with the same value, .Net saves on memory usage by having all references to the same string value actually refer to the same string object. (See this, for instance, for details.)

I don't really know why the ListBox behaves exactly the way it does, which is that the first time you select any item in the list, it selects both that item and the first item in the list. But it doesn't unselect when you click on a new item because checks to see if the SelectedItem is different from the item you just clicked on, and it isn't.

I got exactly the same behavior by binding a ListBox to a collection of test objects:

public class TestObject
{
    public override string ToString()
    {
        return GetHashCode().ToString();
    }
}

In MainWindow.xaml:

<ListBox x:Name="MyListBox" ItemsSource={Binding}"/>

In MainWindow.xaml.cs:

ObservableCollection<TestObject> test = new ObservableCollection<TestObject>();
TestObject t = new TestObject();
test.Add(t);
test.Add(t);
test.Add(t);
test.Add(t);
test.Add(t);
test.Add(t);
MyListBox.DataContext = test;
Robert Rossney
@Robert, accepted for answer-fying Dan's comments and adding some more of your own. apologies Dan, though kudos for being first on the scene. would love to know why ListBox behaves this way when encountering this specific scenario ...
johnny g
@johnny g, I think this is an artifact of the design philosophy behind WPF, which is that a list box is representing a list of distinct data items. You typically would not bind strings directly, but rather data items that render as strings (via a DataTemplate or DisplayMember), which means you should have unique data items, even if the strings rendered are identical in some cases. If you need to select between items with identical representations, you still need to have some data object that encodes the way in which they are different (they must be different or you wouldn't have two options)
Dan Bryant
If non-unique items were an expected use case, `SelectedItem` wouldn't be a sensible property to expose.
Robert Rossney