2014-08-28 45 views
1

我有一個用於treeview的遞歸視圖模型。問題在於我每次刪除組項目或條目項目。在我通過treeview.Items.Refresh()強制執行之前,樹視圖無法立即反映更改。我的猜測是ObservableCollection項目得到通知。你能指出我嗎?謝謝。WPF ViewModel問題

視圖模型

public class Group:ViewModelBase 
{ 
    private int _key; 

    public int Key 
    { 
     get { return _key; } 
     set { _key = value; OnPropertyChanged("Key"); } 
    } 
    private string _name; 

    public string Name 
    { 
     get { return _name; } 
     set { _name = value; OnPropertyChanged("Name"); } 
    } 
    private bool _isexpanded; 

    public bool IsExpanded 
    { 
     get { return _isexpanded; } 
     set { _isexpanded = value; OnPropertyChanged("IsExpanded"); } 
    } 
    private int _order; 

    public int Order 
    { 
     get { return _order; } 
     set { _order = value; OnPropertyChanged("Order"); } 
    } 
    private int _grouporder; 

    public int GroupOrder 
    { 
     get { return _grouporder; } 
     set { _grouporder = value; OnPropertyChanged("GroupOrder"); } 
    } 
    private string _error; 

    public string Error 
    { 
     get { return _error; } 
     set { _error = value; OnPropertyChanged("Error"); } 
    } 
    private ObservableCollection<Group> _subgroups; 

    public ObservableCollection<Group> SubGroups 
    { 
     get { return _subgroups; } 
     set { _subgroups = value; 
      OnPropertyChanged("SubGroups"); 
      OnPropertyChanged("Entries"); 
     } 
    } 
    private ObservableCollection<Entry> _entries; 

    public ObservableCollection<Entry> Entries 
    { 
     get { return _entries; } 
     set { _entries = value; 
      OnPropertyChanged("SubGroups"); 
      OnPropertyChanged("Entries"); } 
    } 


    public ObservableCollection<object> Items 
    { 
     get 
     { 
      ObservableCollection<object> childNodes = new ObservableCollection<object>(); 
      foreach (var entry in this.Entries) 
       childNodes.Add(entry); 
      foreach (var group in this.SubGroups) 
       childNodes.Add(group); 
      return childNodes; 
     } 

    } 
} 

綁定:

public MainWindow() 
    { 
     InitializeComponent(); 
     treeview.ItemsSource = Groups; 
     this.DataContext = this; 


    } 

回答

1

的問題是,你打破與原收藏的鏈接。

所以我可以想像兩種解決方案:

1)報名參加EntriesSubGroups更改並應用他們回到Items

private ObservableCollection<object> items; 
public ObservableCollection<object> Items 
{ 
    get 
    { 
     if (items == null) 
     { 
      items = new ObservableCollection<object>(); 
      foreach (var entry in this.Entries) 
       items.Add(entry); 
      foreach (var group in this.SubGroups) 
       items.Add(group); 


      this.Entries.CollectionChanged += (s, a) => 
      { 
       if (/*add some stuff*/) 
       { 
        items.Add(/*some stuff*/) 
       } 
       else if (/*remove some stuff*/) 
       { 
        items.Remove(...) 
       } 
      }; 

      this.SubGroups.CollectionChanged += ... 

      return items; 
     } 
    } 
} 

而你也將其設置爲每次null重置items您設置了EntriesSubGroups,以便下次重新生成它,同時考慮到新的EntriesSubGroups

2)嘗試用CompositeCollection

new CompositeCollection 
{ 
    new CollectionContainer { Collection = this.Entries }, 
    new CollectionContainer { Collection = this.SubGroups } 
}; 

不知道這第二個解決方案,但它會很優雅,值得一試...

+0

權。永遠不要設置ObservableCollection 屬性,始終調用Clear()和Add(),否則您將覆蓋綁定信息。 – 2014-08-28 20:11:42

+0

@Pragmateek,謝謝!!你救了我的一天。我去了compositecollection! – 2014-08-28 20:13:05

+0

@ JerryNixon-MSFT謝謝! – 2014-08-28 20:13:21