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();

開始すると、ツリービューは問題なく表示され、MyWindow.xaml.cs に追加されたすべての項目が表示されます。

しかし、ボタンをクリックしても、新しいアイテムは表示されません。ツリービューを展開する前にボタンをクリックすると、新しい項目が表示されます...

そのため、ビューは更新されていないようです...

私が間違ったことを誰かが見ることができますか?

4

1 に答える 1

1

Folder クラスの ContentFolders を List<> ではなく ObservableCollection<> に変更します。

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