代码之家  ›  专栏  ›  技术社区  ›  Edward Tanguay

绑定到ObservableCollection时,如何使子树节点出现在WPF树视图中?

  •  0
  • Edward Tanguay  · 技术社区  · 15 年前

    我该怎么办 改变 在下面的代码中, 儿童区 “节点显示为 第一节 第二节 :

    下面的代码给出了一个XamlParseException。

    XAML:

    <Window x:Class="TestTr32322.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TestTr32322"
        Title="Window1" Height="300" Width="300">
        <Window.Resources>
            <HierarchicalDataTemplate x:Key="sectionTemplate" 
                ItemsSource="{Binding ChildSections}"
                DataType="{x:Type local:Section}">
                <TextBlock Text="{Binding Title}" />
            </HierarchicalDataTemplate>
        </Window.Resources>
        <Grid>
            <TreeView ItemsSource="{Binding Sections}" 
                      ItemTemplate="{StaticResource sectionTemplate}">
            </TreeView>
        </Grid>
    </Window>
    

    代码落后:

    using System.Windows;
    using System.ComponentModel;
    using System.Collections.ObjectModel;
    
    namespace TestTr32322
    {
        public partial class Window1 : Window, INotifyPropertyChanged
        {
            #region ViewModelProperty: Sections
            private ObservableCollection<Section> _sections = new ObservableCollection<Section>();
            public ObservableCollection<Section> Sections
            {
                get
                {
                    return _sections;
                }
    
                set
                {
                    _sections = value;
                    OnPropertyChanged("Sections");
                }
            }
            #endregion
    
            public Window1()
            {
                InitializeComponent();
                DataContext = this;
    
                Sections = Section.GetSections();
            }
    
            #region INotifiedProperty Block
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected void OnPropertyChanged(string propertyName)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
    
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(propertyName));
                }
            }
            #endregion
    
        }
    
        public class Section
        {
            public string Title { get; set; }
            public string Description { get; set; }
            public ObservableCollection<Section> ChildSections { get; set; }
    
            public static ObservableCollection<Section> GetSections()
            {
                ObservableCollection<Section> sections = new ObservableCollection<Section>();
    
                {
                    Section section = new Section();
                    section.Title = "First Section";
                    section.ChildSections.Add(new Section
                    {
                        Title = "A Child Section",
                        Description = "this is the description",
                        ChildSections = new ObservableCollection<Section>()
                    });
                    sections.Add(section);
                }
    
                {
                    Section section = new Section();
                    section.Title = "Second Section";
                    section.ChildSections.Add(new Section
                    {
                        Title = "A Child  Section",
                        Description = "this is the description",
                        ChildSections = new ObservableCollection<Section>()
                    });
                    sections.Add(section);
                }
    
                return sections;
            }
        }
    
    }
    
    2 回复  |  直到 15 年前
        1
  •  1
  •   Anvaka    15 年前

    看起来你需要 HierarchicalDataTemplate . 他们在文章中有一个例子…

        2
  •  1
  •   Community CDub    8 年前

    看看这个 question 以及公认的答案。它也应该把你指向正确的方向。

    基本思想是用itemssource属性定义层次结构数据模板,并使它们按类型匹配。

    在你的情况下:

    <HierarchicalDataTemplate DataType="{x:Type local:Section}" 
                              ItemsSource="{Binding ChildSection}">
        <TextBlock Text="{Binding Path=Title}" />
    </HierarchicalDataTemplate>
    

    编辑 : 在代码中,应在向集合添加值之前创建新集合:

    // The new was missing and resulted in the end in your XAML Parse Exception
    section.ChildSections = new ObservableCollection<Section>();
    section.ChildSections.Add( /*... */);
    
    推荐文章