WPF Combobox Updates list but not the selected item
Posted
by JoshReedSchramm
on Stack Overflow
See other posts from Stack Overflow
or by JoshReedSchramm
Published on 2010-03-30T02:47:02Z
Indexed on
2010/03/30
2:53 UTC
Read the original article
Hit count: 486
I have a combo box on a WPF form. On this form the user selects a record from the combo box which populates the rest of the fields on the form so the user can update that record. When they click save I am re-retrieving the combo box source which updates the combo box list. The problem is the selected item keeps the original label even though the data behind it is different. When you expand the combo box the selected item shows the right label.
I am using a command binding mechanism. Here is some of the relevant code.
private void SaveSalesRep()
{
BindFromView();
if (_salesRep.Id == 0)
SalesRepRepository.AddAndSave(_salesRep);
else
SalesRepRepository.DataContext.SaveChanges();
int originalId = _salesRep.Id;
InitSalesRepDropDown();
SalesRepSelItem = ((List<SalesRep>) SalesRepItems.SourceCollection).Find(x => x.Id == originalId);
}
private void InitSalesRepDropDown()
{
var salesRepRepository = IoC.GetRepository<ISalesRepRepository>();
IEnumerable<SalesRep> salesReps = salesRepRepository.GetAll();
_salesRepItems = new CollectionView(salesReps);
NotifyPropertyChanged("SalesRepItems");
SalesRepSelItem = SalesRepItems.GetItemAt(0) as SalesRep;
}
The Selected Item property on the combo box is bound to SalesRepSelItem Property and the ItemsSource is bound to SalesRepItems which is backed by _salesRepItems.
THe SalesRepSelItem property called NotifyPropertyChanges("SalesRepSelItem") which raises a PropertyChanged event.
All told the binding of new items seems to work and the list updates, but the label on the selected item doesnt.
Any ideas? Thanks all.
© Stack Overflow or respective owner