wpf 数据绑定到 XAML 中的 TreeView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12162034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:07:49  来源:igfitidea点击:

Databinding to TreeView in XAML

c#wpfxamldata-bindingmvvm

提问by Konrad Morawski

I am trying to use WPF databinding features to get a TreeViewto display a hierarchical tree of objects (categories).

我正在尝试使用 WPF 数据绑定功能来TreeView显示对象(类别)的分层树。

I have roughly followed this tutorial by Josh Smith, but to no effect: no items appear in my TreeView.

我大致遵循了 Josh Smith 的本教程,但没有效果:我的TreeView.

Here's complete code of my extremely simple application:

这是我极其简单的应用程序的完整代码:

using System.Windows;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {   
            InitializeComponent();
            this.DataContext = CategoriesTreeViewModel.CreateDefault;
        }
    }
}

ViewModel objects and sample data:

ViewModel 对象和示例数据:

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace WpfApplication1
{
    public class Category
    {
        public Category()
        {
            Children = new ObservableCollection<Category>();
        }

        public ObservableCollection<Category> Children
        {
            get;
            set;
        }
        public string Name { get; set; }

    }

    public class CategoriesTreeViewModel
    {
        public ReadOnlyCollection<Category> FirstGeneration;

        private static IEnumerable<Category> SomeCategories
        {
            get
            {
                var A = new Category() { Name = "A" };
                var B = new Category() { Name = "B" };
                var A1 = new Category() { Name = "A1" };
                var A2 = new Category() { Name = "A2" };
                var B1 = new Category() { Name = "B1" };
                var B2 = new Category() { Name = "B2" };

                A.Children.Add(A1);
                A.Children.Add(A2);
                B.Children.Add(B1);
                B.Children.Add(B2);

                yield return A;
                yield return B;
            }
        }

        public static CategoriesTreeViewModel CreateDefault
        {
            get
            {
                var result = new CategoriesTreeViewModel()
                {
                    FirstGeneration = new ReadOnlyCollection<Category>(SomeCategories.ToList())
                };
                return result;                
            }
        }
    }
}

And XAML:

和 XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TreeView ItemsSource="{Binding FirstGeneration}" Name="treeView1">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                    <TextBlock Text="{Binding Name}" />
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </Grid>
</Window>

Why is the TreeViewcontrol blank?

为什么TreeView控件是空白的?

回答by Thelonias

You have no way of accessing your FirstGeneration property. A property without a "get" accessor is considered write-only.

您无法访问您的 FirstGeneration 属性。没有“get”访问器的属性被认为是只写的。

public ReadOnlyCollection<Category> FirstGeneration { get; set; }