2
我有一個簡單的treeview。 itemtemplate有一個複選框。當點擊複選框時,我的模型更新了所有的孩子,以反映與父母相同的選中狀態。這在代碼中工作,但不反映在樹上。我甚至已經添加了一噸callbakcs,屬性更改通知等WPF treeview更新兒童
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));
}
}
}
正如你看到的我甚至增加了一個代表,我更新這會導致視圖模型也叫OnPropertyChange兒童名單後調用()
這個codeproject文章可能會有所幫助:http://www.codeproject.com/KB/WPF/TreeViewWithCheckBoxes.aspx – Argalatyr 2011-02-27 21:07:43