Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF ComboBox resets selected item when item-source changes

Consider the following XAML:

<ComboBox Name="CompanyComboBox" 
    HorizontalAlignment="Stretch"
    ItemsSource="{Binding Path=GlobalData.Companies}" 
    SelectedValuePath="Id"
    SelectedValue="{Binding Customer.CompanyId, ValidatesOnDataErrors=True}"
    DisplayMemberPath="Name" />

GlobalData.Companies is a collection (IEnumerable<Company>) of companies; this collection can be reloaded on background (it is downloaded from a webservice). When this happens, ComboBox correctly reloads items via binding. However as a side-effect, it also resets the selected item!

I have used Reflector to inspect combo-box sources and apparently this is intended behavior.

Is there any "nice" way how to get around this? What I want to achieve, is that if the user selects "Company A" and reloads list of companies afterwards, then "Company A" stays selected (assuming it is in the new list).

like image 559
Miroslav Bajtoš Avatar asked Feb 09 '10 10:02

Miroslav Bajtoš


1 Answers

Please try with the following code. Enable the following property to the combo box

IsSynchronizedWithCurrentItem="True"

like image 155
BOBIN JOSEPH Avatar answered Oct 17 '22 03:10

BOBIN JOSEPH