c# - 在 XAML 中将数据绑定(bind)到 TreeView

标签 c# wpf xaml data-binding mvvm

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

我大致关注过this tutorial by Josh Smith ,但没有效果:我的 TreeView 中没有出现任何项目。

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

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 对象和示例数据:

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;                
            }
        }
    }
}

和 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>

为什么TreeView控件是空白的?

最佳答案

您无法访问您的 FirstGeneration 属性(property)。没有“get”访问器的属性被视为只写。

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

关于c# - 在 XAML 中将数据绑定(bind)到 TreeView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12162034/

相关文章:

c# - 优化 Entity Framework

c# - 在哪里设置 <gcAllowVeryLargeObjects>?

c# - 来自代码 : Change ScrollViewer's scrollbars'-style to touch

c# - 在属性访问器中调用等待方法 [Windows 应用商店应用/Metro 应用]

xaml - 如何动画化元素的出现

c# - 检查枚举是否处于所需状态之一

c# - 将以编程方式生成的 DataGridTextColumn 的 Visibility 属性绑定(bind)到复选框

c# - Wpf DataGrid(德语)日期排序

c# - 如何部署 "SQL Server Express + EF"应用程序

WPF - 将许多图像一张一张地加载到 ObservableCollection 中