【发布时间】:2018-08-19 12:43:39
【问题描述】:
我有一个树视图,当某些项目标签发生更改时,它应该应用于所有子项。 我使用以下方法将标签应用于儿童:
public void SetCLassificationForChildren(TreeItemViewModel item, Labels label)
{
if (item == null) return;
item.Label = label;
item.Children?.ForEach(c => SetCLassificationForChildren(c, label));
}
这是一个 Label-property 和 INotifyPropertyChanged 的实现:
...
public Labels Label
{
get => _label;
set
{
_label = value;
OnPropertyChanged(() => Label);
}
}
...
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged<T>(Expression<Func<T>> propertyExpression)
{
var body = propertyExpression.Body as MemberExpression;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(body.Member.Name));
}
所以,当属性获得标签值时,会执行以下操作:
_label = value - 工作正常
但是,之后是下一行:
OnPropertyChanged(() => Label) 再次引发设置代码并设置旧值。
我不知道为什么会这样。
UPD“标签”是我的枚举类型:
public enum Labels
{
NotClassified = 0,
Internal,
Confidential,
StrictlyConf
}
【问题讨论】:
标签: c# wpf mvvm inotifypropertychanged