views:

125

answers:

1

Can anyone explain how silverlight/wpf databinding actually determines which is the right item to set as the selectedItem in a collection as in the following snippet

<ComboBox SelectedItem="{Binding Mode=TwoWay, Path=Province}" 
           ItemsSource="{StaticResource ProvinceCollection}" />

Where ProvinceCollection is simply a static array of strings.

In this example the items are strings and therefore value types which are simple enough but what how does it know what to set as the selected item when the collection contains something more complicated like object instances loaded at runtime. The object references of the (Datacontext).Province and the items in the ProvinceCollection are NOT going to be the same even if I think they should be.

Im tring to do just this, get the databinding to select a item from a collection where the current datacontext's item is not in the collection (ie there is no item that has the same object reference)

+1  A: 

I've hit this nasty one myself, in Silverlight 2 unfortunately it's using Object.ReferenceEquals (checked in reflector), so it actually has to be the same object. This is incredibly irritating. Nothing you can do about it I'm afraid :(

This has been fixed in Silverlight 3 (well the Beta at least) where it is now uses Equals, and thus you can use the trick of overriding the Equals method.

mattmanser