我有一个简单的树视图。 itemtemplate有一个复选框。单击复选框后,我的模型会更新所有子项以反映与父项相同的已检查状态。这适用于代码,但不会反映在树上。我甚至添加了大量的callbakcs,财产变更通知等。
public class OutlookFolder : INotifyPropertyChanged
{
public delegate void CollectionChangedDelegate();
public static CollectionChangedDelegate CollectionChanged;
public string Name { get; set; }
public string ID { get; set; }
private bool _checked;
public bool Checked
{
get { return _checked; }
set { _checked = value; UpdateChildren(value); }
}
private ObservableCollection<OutlookFolder> _Children;
public ObservableCollection<OutlookFolder> Children
{
get { return _Children; }
set { _Children = value; OnPropertyChanged("Children"); }
}
private void UpdateChildren(bool value)
{
if (Children == null) { return; }
foreach (OutlookFolder f in Children)
{
f.Checked = value;
}
if (CollectionChanged != null)
{
CollectionChanged.Invoke();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(name));
}
}
}
正如你所看到的,我甚至添加了一个在更新子列表后调用的委托,这导致viewmodel也调用OnPropertyChange()
答案 0 :(得分:2)
更改Checked时,您的OutlookFolder对象是否实现了PropertyChanged? Collectionchanged不会仅更新树视图中的单个项目。