本文介绍了TreeView 不显示存储在数据中的子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中,我有一个带有数据支持(视图模型和数据模型)的 TreeView.添加到 TreeView 的每个项目都会添加一个子项目,但由于某种原因,子项目在运行时没有反映.但是,我能够在调试过程中看到子项,并且 TreeView's 数据模型中的Children"属性在它的 setter 中包含 NotifyPropertyChange.

In my program I have a TreeView with data backings (View Model & Data Model). Each item added to the TreeView gets a child item added with it, but for some reason the children are not reflecting during run time. However, I am able to see the children during debugging and my "Children" property in the TreeView's data model includes NotifyPropertyChange in it's setter.

注意:之前我有一个类似的问题,即新添加的节点(这些子节点的父节点)也不会显示.此问题已通过在树的 xaml 中设置 DisplayMemberPath="DisplayName.Value" 得到纠正.

Note: Earlier I had a similar problem where newly added nodes (parents of these children) wouldn't be displayed either. This problem was corrected by setting DisplayMemberPath="DisplayName.Value" in the tree's xaml.

TreeView 的 xaml:

TreeView ItemsSource="{Binding UserControl_DataModel.TreeViewViewModel.ObservableCollection<TreeViewDataModel>}" DisplayMemberPath="DisplayName.Value".../>

这是创建新节点并将其添加到TreeView (TreeViewViewModel) 的地方:

This is where new nodes are created and added to the TreeView (TreeViewViewModel):

private TreeViewDataModel createNewNode(StringItem nodeName)
{
    var newNode = new TreeViewDataModel ()
    {
        DisplayName = nodeName
    };

    newNode.Children.Add(new TreeViewDataModel () { DisplayName = nodeName});

    return newNode;
}

public void addNewLocNode(StringItem nodeName)
{
    TreeObservableCollection.Add(createNewNode(nodeName));
}

注意:StringItem 是一个自定义类.在StringItem 中,Value 返回项目的字符串值.(例如:StringItem.Value)

Note: StringItem is a custom class. In StringItem, Value returns the string value of the item. (Ex: StringItem.Value)

TreeViewDataModel 中的子属性:

Children property in TreeViewDataModel:

public ObservableCollection<TreeViewDataModel> Children
{
    get { return _children ?? (_children = new ObservableCollection<TreeViewDataModel>()); }
    set
    {
        _children = value;
        NotifyPropertyChange(() => Children);
    }
}

为什么我的 TreeView 中没有显示子节点,我该如何解决?

Why aren't the child nodes showing up in my TreeView, and how can I fix this?

推荐答案

你没有足够的代码让我知道你是如何设置的.我试图让它与你已经拥有的相似.不过我想你应该能够弄清楚.

You don't have enough code for me to know exactly how you have things set up. I tried to keep it similar to what you already have. I think you should be able to figure it out though.

截图:

XAML:

<Window x:Class="WpfApplication9.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:app="clr-namespace:WpfApplication9"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <TreeView ItemsSource="{Binding TreeObservableCollection}"> <!-- DisplayMemberPath="DisplayName.Value" - Can't set both DisplayMemberPath and ItemTemplate -->
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate DataType="{x:Type app:TreeViewDataModel}" ItemsSource="{Binding Children}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding DisplayName.Value}" />
                </StackPanel>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>
</Grid>

代码:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication9
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public ObservableCollection<TreeViewDataModel> TreeObservableCollection { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            TreeObservableCollection = new ObservableCollection<TreeViewDataModel>();
            this.DataContext = this;
            this.Loaded += MainWindow_Loaded;
        }
        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            addNewLocNode(new StringItem("A"));
            addNewLocNode(new StringItem("B"));
            addNewLocNode(new StringItem("C"));
            addNewLocNode(new StringItem("D"));
        }
        private TreeViewDataModel createNewNode(StringItem nodeName)
        {
            var newNode = new TreeViewDataModel()
            {
                DisplayName = nodeName
            };
            newNode.Children.Add(new TreeViewDataModel() { DisplayName = nodeName });
            return newNode;
        }

        public void addNewLocNode(StringItem nodeName)
        {
            TreeObservableCollection.Add(createNewNode(nodeName));
        }
    }
    public class StringItem
    {
        public string Value { get; set; }
        public StringItem(string val)
        {
            Value = val;
        }
    }
    public class TreeViewDataModel : System.ComponentModel.INotifyPropertyChanged
    {
        public StringItem DisplayName { get; set; }
        private ObservableCollection<TreeViewDataModel> _children;
        public ObservableCollection<TreeViewDataModel> Children
        {
            get { return _children ?? (_children = new ObservableCollection<TreeViewDataModel>()); }
            set
            {
                _children = value;
                NotifyPropertyChange("Children");
            }
        }
        private void NotifyPropertyChange(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(name));
        }
        #region INotifyPropertyChanged Members

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}

替代方式:

<Window x:Class="WpfApplication9.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:app="clr-namespace:WpfApplication9"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <HierarchicalDataTemplate DataType="{x:Type app:TreeViewDataModel}" ItemsSource="{Binding Children}">
        <TextBlock Text="{Binding DisplayName.Value}" />
    </HierarchicalDataTemplate>
</Window.Resources>
<Grid>
    <TreeView ItemsSource="{Binding TreeObservableCollection}" />
</Grid>

这篇关于TreeView 不显示存储在数据中的子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 01:10