2011-02-27 114 views
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兒童名單後調用()

+0

這個codeproject文章可能會有所幫助:http://www.codeproject.com/KB/WPF/TreeViewWithCheckBoxes.aspx – Argalatyr 2011-02-27 21:07:43

回答

2

更改Checked時,OutlookFolder對象是否實現PropertyChanged? Collectionchanged不會僅更新單個項目的樹視圖。

+0

該死的,我怎麼能忘記。謝謝。 – 2011-02-27 21:02:50