11

我在后面的代码中手动添加 TreeViewItems 并想使用 DataTemplate 来显示它们,但不知道如何。我希望做这样的事情,但项目显示为空标题。我究竟做错了什么?

XAML

<Window x:Class="TreeTest.WindowTree"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WindowTree" Height="300" Width="300">
    <Grid>
        <TreeView Name="_treeView">
            <TreeView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=Name}" />
                        <TextBlock Text="{Binding Path=Age}" />
                    </StackPanel>
                </DataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </Grid>
</Window>

代码背后

using System.Windows;
using System.Windows.Controls;

namespace TreeTest
{
    public partial class WindowTree : Window
    {
        public WindowTree()
        {
            InitializeComponent();

            TreeViewItem itemBob = new TreeViewItem();
            itemBob.DataContext = new Person() { Name = "Bob", Age = 34 };

            TreeViewItem itemSally = new TreeViewItem();
            itemSally.DataContext = new Person() { Name = "Sally", Age = 28 }; ;

            TreeViewItem itemJoe = new TreeViewItem();
            itemJoe.DataContext = new Person() { Name = "Joe", Age = 15 }; ;
            itemSally.Items.Add(itemJoe);

            _treeView.Items.Add(itemBob);
            _treeView.Items.Add(itemSally);
        }
    }

    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}
4

2 回答 2

12

您的 ItemTemplate 正在尝试在 TextBlocks 中呈现“名称”和“年龄”属性,但 TreeViewItem 没有“年龄”属性,并且您没有设置其“名称”。

因为您使用的是 ItemTemplate,所以无需将 TreeViewItems 添加到树中。相反,直接添加您的 Person 实例:

_treeView.Items.Add(new Person { Name = "Sally", Age = 28});

当然,问题在于您的底层对象(“Person”)没有任何层次结构的概念,因此没有简单的方法可以将“Joe”添加到“Sally”。有几个更复杂的选项:

您可以尝试处理 TreeView.ItemContainerGenerator.StatusChanged 事件并等待生成“Sally”项目,然后获取它的句柄并直接添加 Joe:

public Window1()
{
    InitializeComponent(); 
    var bob = new Person { Name = "Bob", Age = 34 }; 
    var sally = new Person { Name = "Sally", Age = 28 }; 

    _treeView.Items.Add(bob); 
    _treeView.Items.Add(sally);

    _treeView.ItemContainerGenerator.StatusChanged += (sender, e) =>
    {
        if (_treeView.ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated) 
            return;

        var sallyItem = _treeView.ItemContainerGenerator.ContainerFromItem(sally) as TreeViewItem;
        sallyItem.Items.Add(new Person { Name = "Joe", Age = 15 });
    };
}

或者,更好的解决方案是,您可以将层次概念引入“Person”对象并使用 HierarchicalDataTemplate 来定义 TreeView 层次:

XAML:

<Window x:Class="TreeTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WindowTree" Height="300" Width="300">
    <Grid>
        <TreeView Name="_treeView">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Subordinates}">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=Name}" />
                        <TextBlock Text="{Binding Path=Age}" />
                    </StackPanel>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </Grid>
</Window>

代码:

using System.Collections.Generic;
using System.Windows;

namespace TreeTest
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent(); 
            var bob = new Person { Name = "Bob", Age = 34 }; 
            var sally = new Person { Name = "Sally", Age = 28 }; 

            _treeView.Items.Add(bob); 
            _treeView.Items.Add(sally);
            sally.Subordinates.Add(new Person { Name = "Joe", Age = 15 });
        }

    }
    public class Person 
    {
        public Person()
        {
            Subordinates = new List<Person>();
        }

        public string Name { get; set; } 
        public int Age { get; set; }
        public List<Person> Subordinates { get; private set;  }
    }
}

这是一种更“面向数据”的方式来显示您的层次结构和更好的方法恕我直言。

于 2009-07-23T23:49:01.317 回答
0

如果您将 DataTemplate 从 TreeView 中拉出并将其放入 Window.Resources,它将起作用。像这样:

<Window.Resources>    
    <DataTemplate DataType={x:type Person}>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding Path=Name}" />
            <TextBlock Text="{Binding Path=Age}" />
        </StackPanel>
    </DataTemplate>
</Window.Resources>

不要忘记在 Person 之前添加正确的命名空间。

于 2016-06-24T13:08:53.603 回答