0
我想要一個項目列表,可以顯示項目的'希臘'或'英文'的名稱,具體取決於用戶之間切換兩者。列表中的所有項目均實施INPC。ObservableCollection項目不顯示更新
由於每個項目都有一個GreekName屬性和一個RomanName屬性,所以我使用的策略是簡單地更改項目DisplayName屬性。單元測試和日誌輸出表明每個項目的DisplayName確實發生了改變並且會觸發INPC,但是列表不會更新。
該列表是一個ObservableCollection。我想知道這是否因爲哈希代碼沒有更改而無法更新?這是否意味着用新的替換列表中的項目的唯一方法?
下面的一些代碼...
乾杯,
Berryl
public 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);
NotifyOfPropertyChange(() => UseRomanName);
}
}