Frage

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?

War es hilfreich?

Lösung

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);
    }

}

Andere Tipps

In case Nicholas does not answer.

What I suspect is that multiple items have the the same KeyValue.

I suggest you check your configuration to generate unique KeyValue as duplicate keys could cause other errors.
Wrapping it is just masking the problem.

When you select an item in the UI it compares that to the currently selected item.
If they are the Equal it assumes that item is already selected and does nothing.

When you wrapped it you got GetHashCode and Equals from Object and object gives the correct answer (every object is unique).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top