ObservableCollection items not showing updates
I want a list of items that can display either an item's 'greek' or 'english' name, depending on a user toggling between the two. All of the items in the list implement INPC.
Since each item has a GreekName property and an RomanName property, the strategy I am using is to simply change the items DisplayName property. Unit tests and log output indicate that the DisplayName for each item does change and does fire INPC, but the list does not update.
The list is an ObservableCollection. I am wondering if this fails to update because the hash code doesn't change? Does that mean that the only way to replace the item in the list with a new one?
Some code below...
Cheers,
Berrylpublic class MasterViewModel : ViewModelBase
{
public ObservableCollection<DetailVm> AllDetailVms
{
get { return _allDetailVms; }
}
private readonly ObservableCollection<DetailVm> _allDetailVms;
void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
if (e.NewItems != null && e.NewItems.Count != 0)
foreach (DetailVm vm in e.NewItems) vm.PropertyChanged += OnGreekGodChanged;
if (e.OldItems != null && e.OldItems.Count != 0)
foreach (DetailVm vm in e.OldItems) vm.PropertyChanged -= OnGreekGodChanged;
}
private void OnGreekGodChanged(object sender, PropertyChangedEventArgs e)
{
var detailVm = (DetailVm)sender;
// if DisplayName has changed we want to refresh the view & its filter
var displayName = ExprHelper.GetPropertyName<DetailVm>(x => x.DisplayName);
if (e.PropertyName == displayName)
Log.Info("'{0} reports it's display name has changed", detailVm.DisplayName);
}
private void _flipGreekOrRomanDisplay(string newName, Func<DetailVm, string> property)
{
foreach (var detailVm in _allDetailVms)
{
Log.Info("To '{0}', before change: '{1}'", newName, detailVm.DisplayName);
detailVm.DisplayName = property(detailVm);
Log.Info("To '{0}', after change: '{1}'", newName, detailVm.DisplayName);
}
NameFilterLabelText = newName;
NotifyOfPropertyChange(() => NameFilterLabelText);
NotifyOfPropertyChange(() => UseGreekName);
NotifyOfPropertyChan开发者_开发问答ge(() => UseRomanName);
}
}
My idiocy - my databinding was off. The code was fine and the view updates by virtue of it's items firing INPC, as expected.
精彩评论