Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF ComboBox: Wrong Item is displayed

This is the initial situation:

XAML:

<ComboBox Grid.Row="0"
           Grid.Column="1"
           Margin="0,3"
           HorizontalAlignment="Stretch"
           DisplayMemberPath="DisplayText"
           ItemsSource="{Binding ObjectSource}" />

ViewModel:

public Collection<MyObjects> ObjectSource
{
    get
    {
        return this.objectSource;
    }

    set
    {
        this.SetProperty(ref this.objectSource, value);
    }
}

My Objects contains a name (string), valid from (dateTime) and a displayText (string only get) which combine the name and valid from for displaying.

In this easy situation I am able to open the combobox an see all entries, after selecting one it also display the right displaytext inside the combobox. Now I open the the dropdown area again and select an other entry. The result is that the slected item switched (as you can see the highligthed item when open the dropdown entry again). But the displayed item inside the combobox does not changed, there is still the DisplayText of the first selection.

Screenshot of the result situation

Does anybody has an idea for me why the combobox does not update? Thanks in advance

Edit: Thanks all for their help. Problem was a buggy overriding of Equals.

like image 832
Sukram Avatar asked Jul 16 '13 12:07

Sukram


1 Answers

just for completeness :)

you have to check your Equals() override and make sure thats not buggy. i had the same problem with a listbox these days.

like image 129
blindmeis Avatar answered Oct 03 '22 00:10

blindmeis