views:

145

answers:

1

Hi,

I've got combo box bound to a custom collection type - its basically an overridden ObservableCollection which I've added a facility to update the underlying collection (via Unity).

I don't want to confuse the issue too much, but thats the background.

My xaml looks like this

<ComboBox ItemsSource="{Binding Manufacturers}" DisplayMemberPath="Name"  SelectedValuePath="ID" SelectedValue="{Binding Vehicle.ManufacturerID}" />

And in my overridden collection i was doing this.

var index = IndexOf(oldItem);
this[index] = (T)newItem;

I had hoped because it was bound by value, that inserting the new object(which had the same id) over the old object would work. But it seems that although its bound by SelectedValue it still knows that its being swapped for a different one. The combo just looses its selection.

Can anyone help please?

A: 

I assume that your oldItem was the SelectedValue in this case?

When you remove this item, your binding gets updated instantly. So removing that value from the list will clear your selection.

If you want to replace the item, you might want to try to get the SelectedValue, save it in a variable, and set the SelectedValue later when you have replaced your item, like so:

object oldValue = SelectedValue;
//Do awesome stuff to replace things
SelectedValue = oldValue;

HTH

Arcturus
Hi, thanks for your help, but because my code is within my overridden ObservableCollection I don't have access to the SelectedValue of the combo box. Any other ideas?
Andy Clarke
Hmm you're right.. can you 'update' the existing item with the new property values?
Arcturus
Funnily enough I'd just had the same thought and used reflection to copy the properties across. Seems to work pretty well!
Andy Clarke
Nice nice.. I agree though, it should not be this way..
Arcturus