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 类有一个属性 Folder 并添加了一些内容。如果按钮被点击,它还有一个方法来添加一个新文件夹。

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 将由一个简单的 Main 方法调用:

    new MyWindow();

如果我开始,treeview 看起来不错,它包含所有已添加到 MyWindow.xaml.cs 中的项目。

但是如果我单击按钮,将不会显示新项目。如果我在展开树视图之前单击按钮,新项目将在那里......

所以视图似乎没有更新......

谁能看到,我做错了什么?

4

1 回答 1

1

将文件夹类中的 ContentFolders 更改为 ObservableCollection<> 而不是 List<>

    public ObservableCollection<Folder> ContentFolders { get; set; }
于 2012-11-02T14:39:22.610 回答