【问题标题】:Display Hierarchical Data in TreeView在 TreeView 中显示分层数据
【发布时间】:2015-07-06 09:36:39
【问题描述】:

我只是想在 TreeView 中显示分层数据,但我只是不知道如何使它显示比前两个级别更多。 (而且我几乎已经阅读了所有 TreeView 帖子,也许问题是我在这种情况下对绑定的(错过)轻描淡写)

我已经为这个测试简化了我的数据结构:

public class Node
{
    public List<Node> Children { get; set; }

    public Node Parent { get; set; }

    public string Expression { get; set; }
}

Xaml 目前看起来是这样的:(请注意我现在已经更改了好几次,但这是我想出的原始状态:)

<Window x:Class="Klammern_Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Klammern_Test"
        Title="MainWindow" Height="439" Width="402">
    <Window.Resources>
        <HierarchicalDataTemplate DataType="{x:Type local:Node}">
            <TreeViewItem ItemsSource="{Binding Children}" Header="{Binding Expression}"/>
        </HierarchicalDataTemplate>
    </Window.Resources>
    <Grid>
        <TreeView ItemsSource="{Binding Root}" Margin="12,41,12,12" Name="treeView" />            
    </Grid>
</Window>

这就是我尝试填充树视图的方式:

public partial class MainWindow : Window
{
        public Node Root { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;

        }

        private void StartButton_Click(object sender, RoutedEventArgs e)
        {
            Parser = new StringParser();
            Root = Parser.Parse(Tbx_Eingabe.Text);       
            treeView.Items.Add(PopulateTreeView(Root));



        }

        private TreeViewItem PopulateTreeView(Node node)
        {
            TreeViewItem treeViewItem = new TreeViewItem();
            treeViewItem.IsExpanded = true;
            treeViewItem.Header = node.Expression;


            foreach (Node child in node.Children)
            {
                treeViewItem.Items.Add(new TreeViewItem() { Header = child.Expression });

                if (child.Children.Count > 0)
                {
                    PopulateTreeView(child);
                }
            }

            return treeViewItem;
        }
}

我错过了什么?

编辑:

在尝试了 almulo 的提示后,我用 Snoop-tool 找到了这个,但我完全不知道它是什么意思,我没有找到其他红线,并且根本没有进入 Binding Errors 列。

【问题讨论】:

  • Parser.Parse() 返回什么? Root是什么类型的?
  • 它返回一个NodeRoot也是一个Node
  • 嗯,这可能是问题所在...ItemsSource 期待收集,Root 是单个 Node。我将在下面编辑我的答案。
  • 哦,所以当我将 Root 更改为 List 时也许它会起作用?
  • 很高兴读到:)不客气!

标签: wpf .net-4.0 treeview hierarchicaldatatemplate


【解决方案1】:

HierarchicalDataTemplate 中使用 TreeViewItem 是您的 TreeView 的 TreeViewItems 将用来创建自己的内容......令人困惑。

在您的HierarchicalDataTemplate 中,您应该只添加您希望项目“标题”具有的控件。在这种情况下,我想应该是 TextBlock,因为您只是想显示一些文字。

然后使用HierarchicalDataTemplate.ItemSource 属性绑定节点的子节点。

    <Window.Resources>
        <HierarchicalDataTemplate DataType="{x:Type local:Node}" 
                                  ItemsSource="{Binding Children}">
            <TextBlock Text="{Binding Expression}" />
        </HierarchicalDataTemplate>
    </Window.Resources>

此外,在您的代码隐藏中,您不应直接操作 TreeView.ItemsTreeViewItem.Items,因为您已经在使用 Bindings 和 ItemsSource 属性。

相反,删除 PopulateTreeView 方法并让您的 Root 属性作为 TreeView 的项目源。但为了使其工作,您必须在 Root 属性更改其值时通知视图。

为此,请实现INotifyPropertyChanged 接口并在每次Root 更改时触发PropertyChanged 事件。

编辑: ItemsControl 属性ItemsSource 需要一个集合(更具体地说,是一个IEnumerable),所以Root 必须是一个集合。即使它只有一个项目,像这样:

public class MainWindow : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    // ...

    private void StartButton_Click(object sender, RoutedEventArgs e)
    {
        Parser = new StringParser();
        Root = new Node[] { Parser.Parse(Tbx_Eingabe.Text) };

        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs("Root"));
    }

    // ...
}

【讨论】:

  • 好吧,你肯定是在覆盖你的绑定。没有意识到您正在直接访问 TreeView 的 Items 属性...实际上,这可能会引发异常,因为您不应该能够同时使用 ItemsSourceItems
  • 我将扩展我的答案,建议对您的代码隐藏进行一些更改。
  • 这很奇怪,我刚刚复制了你的代码,它马上就可以工作了……我只需要创建我自己的“解析器”,它只创建了一个最多三层深度的节点列表。我猜你的解析器也返回了一个正确的列表,所以我不确定可能出了什么问题......
  • 至于使用DataContext = this,通常不建议这样做。但在一个不被重用的独立视图中,它可能没问题。
  • 使用 Snoop 或检查 VS 的输出选项卡来查找绑定错误。可能是您的绑定无法正常工作,阅读实际的错误消息可能会更清楚地说明问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-27
  • 1970-01-01
相关资源
最近更新 更多