Search code examples
c#wpfcomboboxsubsonicequals

List<T> of particular type causes ComboBox to break


I have a List<T> which holds entity types generated by SubSonic. Let's call this type NaughtyItem. When the list of NaughtyItem is bound to the ComboBox, strange things start to happen*. The ComboBox is bound one way to the collection like so:

<ComboBox  ItemsSource="{Binding Model.NaughtyCollection,Source={StaticResource ModelSource}}" Width="120" Margin="0,0,10,0" />

*The first time an item is selected, the selection changes. Once I have changed the item though, I can't select items again. I subscribed to the SelectionChanged event, and it fires ones and that is it. I've solved the problem by creating a wrapper type

public class NaughtyWrapper
{
   public NaughtyItem {get;set;}
}

My guess would be that something NaughtyItem is doing is causing the ComboBox to crash and burn. I've turned on all CLR exceptions in VisualStudio, but I don't get any interesting exceptions that would explain why the unwrapped NaughtyItems are causing issues.

Anyone come across anything like this before?


Solution

  • All credit goes to Nicolas Repiquet for the answer.

    The entities in SubSonic have a custom implementation of Equals() which is the cause of this problem. Deleting the custom implementation solves the issue, though I will still use my original solution of the wrapped SubSonic entities since I don't want to mess with the framework.

    public override bool Equals(object obj){
        if(obj.GetType()==typeof(NaughtyItem)){
            NaughtyItem compare=(NaughtyItem)obj;
            return compare.KeyValue()==this.KeyValue();
        }else{
            return base.Equals(obj);
        }
    

    }