2012-11-02 37 views
2

我想使用數據綁定在樹視圖中查看文件夾結構。 文件夾類只有一個兒童屬性列表和一個屬性名稱。樹視圖上的遞歸數據綁定

如果有什麼改變,它會觸發相應的事件。 這是它:

public class Folder : INotifyPropertyChanged, INotifyCollectionChanged 
    {  
     public event PropertyChangedEventHandler PropertyChanged;   
     public event NotifyCollectionChangedEventHandler CollectionChanged; 

     public Folder(string name) 
     { 
      this.Name = name; 
      this.ContentFolders = new List<Folder>(); 
     } 

     public List<Folder> ContentFolders { get; set; } 

     public void AddFolder(Folder f) 
     { 
      this.ContentFolders.Add(f); 

      if (this.CollectionChanged != null) 
      { 
       this.NotifyCollectionChanged(
         new NotifyCollectionChangedEventArgs(
          NotifyCollectionChangedAction.Add, f)); 
      } 
      this.PropertyChanged(this, new PropertyChangedEventArgs("ContentFolders")); 
     } 

     private void NotifyCollectionChanged(NotifyCollectionChangedEventArgs e) 
     { 
      lock (CollectionChanged) 
      { 
       if (CollectionChanged != null) 
       { 
        Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() => CollectionChanged(this, e))); 
       } 
      } 
     } 

     private string name; 

     public string Name 
     { 
      get 
      { 
       return this.name; 
      } 
      set 
      { 
       if (this.name != value) 
       { 
        this.name = value; 
        if (PropertyChanged != null) 
        { 
         PropertyChanged(
          this, new PropertyChangedEventArgs("Name")); 
        } 
       } 
      } 
     } 

    } 

這是我的GUI,其中顯示了樹視圖中的根文件夾:

<Window x:Class="WpfApplication2.MyWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfApplication2="clr-namespace:WpfApplication2" 
      Title="MyWindow" Height="300" Width="300" xmlns:u="clr-namespace:UpdateControls.XAML;assembly=UpdateControls.XAML"> 
     <StackPanel> 
     <StackPanel.Resources> 
      <HierarchicalDataTemplate DataType="{x:Type WpfApplication2:Folder}" 
            ItemsSource="{Binding Path=ContentFolders}"> 
      <TextBlock Text="{Binding Path=Name}" /> 
      </HierarchicalDataTemplate> 
     </StackPanel.Resources> 
     <TreeView Name="TreeviewScenario"> 
      <TreeViewItem Header="{Binding Path=RootFolder.Name}" 
         ItemsSource="{Binding Path=RootFolder.ContentFolders}" /> 
     </TreeView> 
     <Button Content="Add Folder" Click="Button_Click" /> 
     </StackPanel> 
    </Window> 

的根據MyWindow.xaml.cs類有一個屬性的文件夾,並增加了一些內容。它也有一個方法讓按鈕添加一個新的文件夾,如果它被點擊。

public partial class MyWindow : Window 
{ 
    public Folder RootFolder { get; set; } 

    public MyWindow() 
    { 
     this.RootFolder = new Folder("root"); 
     this.RootFolder.ContentFolders.Add(new Folder("1")); 
     this.RootFolder.ContentFolders.Add(new Folder("12")); 
     this.RootFolder.ContentFolders.Add(new Folder("13")); 
     this.RootFolder.ContentFolders.Add(new Folder("14")); 
     this.RootFolder.ContentFolders.Add(new Folder("15")); 

     Folder aFolder = new Folder("aFolder"); 
     aFolder.ContentFolders.Add(new Folder("2")); 
     aFolder.ContentFolders.Add(new Folder("21")); 
     aFolder.ContentFolders.Add(new Folder("22")); 
     aFolder.ContentFolders.Add(new Folder("23")); 
     aFolder.ContentFolders.Add(new Folder("24")); 

     this.RootFolder.ContentFolders.Add(aFolder); 

     this.DataContext = this; 
     InitializeComponent(); 
    } 

    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     Folder c = new Folder("a new Folder"); 
     this.RootFolder.AddFolder(c); 
    } 
} 

GUI會被卡列斯通過簡單的方法主要有:

new MyWindow(); 

如果我做起,樹視圖看起來不錯,它擁有所有的項目,已在MyWindow.xaml被添加的.cs。

但是,如果我點擊按鈕,將不會顯示新項目。如果我按一下按鈕之前,我展開樹視圖,新的項目會在那裏......

所以認爲似乎並沒有被更新中...

任何人都可以看到,我做了什麼錯?

回答

1

更改您的文件夾類ContentFolders到一個ObservableCollection>的<,而不是一個列表<>

public ObservableCollection<Folder> ContentFolders { get; set; }