ComboBox’s SelectedItem not Displaying

I see a lot of posts on StackOverflow about WPF ComboBoxes not displaying the SelectedItem correctly.

The code given usually looks something like this:

<ComboBox ItemsSource="{Binding MyList}"
          SelectedItem="{Binding MyItem}" />

The problem is simple: By default WPF compares SelectedItem to each item in the ItemsSource by reference, meaning that unless the SelectedItem points to the same item in memory as the ItemsSource item, it will decide that the item doesn’t exist in the ItemsSource and so no item gets selected.

To work around this, you can either use the ComboBox’s SelectedValue and SelectedValuePath to set the SelectedItem by Value instead of by Item

<ComboBox ItemsSource="{Binding MyList}"
          SelectedValue="{Binding SelectedId}" />

Or you can overwrite the .Equals() method of the MyItem class so that it returns true if two item’s have the same data, not just the same memory reference.

public override bool Equals(object obj) 
    if (obj == null || !(obj is MyClass)) 
        return false; 
    return ((MyClass)obj).Id == this.Id); 

16 Responses to ComboBox’s SelectedItem not Displaying

  1. Thank you!! this post helped me.

  2. Thank you so much! I’ve been looking at my project for two hours and your post eventually helped me out!

  3. Oliver Fehr says:

    Overriding Equals() was the solution for me! Thanks!

  4. Matias says:

    This post saved mi life, I searched everywhere and you describe my problem just as it is. Thanks a lot Rachel. You are fantastic.

  5. Rajib says:

    Hi Rachel,
    I am trying to fix someone else’s code that suffers from this bug. The combo box is not populating with the selected item. I have overridden the Equals method in my item class, but that does not solve the problem. I’ve pasted some code below. Any insight that you can provide is highly appreciated.

    — Here is the code for the equals method
    public override bool Equals(object obj)
    if (obj == null || !(obj is Dose))
    return false;

    return ((Dose)obj).Id == this.Id;

    • Rachel says:

      Hi Rajib, try posting a question on with the relevant XAML and code-behind. Unfortunately what you’ve posted here isn’t enough for me to help you with your problem. Thanks, Rachel

  6. Mårten says:

    Thank you! I spent nearly 15 minutes being annoyed by this before finding this post🙂

  7. Morgan says:

    Thank you for this post, it literally saved my day !!

  8. Travis says:

    Thanks for this solution. Can you tell me how exactly I can do it the SelectedValue and SelectedValuePath in my example (on StackOverflow)? :

    • Rachel says:

      Hi Travis, I left a comment on your SO post about that, and have also updated my post here to give a short code sample. If it’s still unclear, let me know.


    • Daniel unsworth says:

      Followed you example on stackoverflow works very well thank you was stuck on this🙂

  9. Travis says:


    I used your idea of override Equals for my combobox SelectedItem not updateing issue. I would like to know how to actually get the alternative method using SelectedValue and SelectedValuePath to work in my example. I have tried several times, and just can’t get it! And, which method is “better”? I was told I should also override GetHashCode if I override Equals(), is this true or needed?

    My OP concerning this is on StackOverflow @


    • Rachel says:

      Personally I think which method is “better” depends on how your data is structured. I prefer to use SelectedValue instead of SelectedItem in most cases, but both methods are viable.

      And yes, if you override .Equals() you should probably overwrite .GetHashCode() too. See this SO question for more information about why.

  10. Vikram says:

    Thanks a lot for the post🙂

  11. xiaoyuanfxy says:

    Thanks for this post. It really helped me out🙂

  12. Vesela Gerasimova says:

    This is great post, saved me lots of work and depression🙂 One more thing: in Equals method be careful to compare only value types.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: