wpf treeview mvvm

use*_*474 3 wpf treeview mvvm

我试图使用mvvm填充树视图,但树不显示任何数据.我有一个员工列表,这是我的虚拟机中的一个属性,其中包含员工数据.xaml如下.

                <Style TargetType="{x:Type TreeViewItem}">
                    <Setter Property="FontWeight" Value="Normal" />

                </Style>
            </TreeView.ItemContainerStyle>

            <TreeView.ItemTemplate>

                <HierarchicalDataTemplate ItemsSource="{Binding EmpList}" >
                    <TextBlock Text="{Binding EmpName}"/>

                </HierarchicalDataTemplate>

            </TreeView.ItemTemplate>

        </TreeView>
Run Code Online (Sandbox Code Playgroud)

这里有什么我想念的吗?

谢谢

Dab*_*rnl 7

嗨伊恩的建议文章确实很棒!

诀窍是您应该指定Treeview如何通过特定类型(Hierarchical)DataTemplates显示其项目.您可以在Treeview的资源中指定这些数据窗口(如果要在更多树视图中重用它们,则在可视化树的更高位置).

我试图模拟你想要的东西:

<Window x:Class="TreeViewSelection.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TreeViewSelection"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
        <TreeView ItemsSource="{Binding Enterprises}">
            <TreeView.Resources>
                <!-- template for showing the Enterprise's properties
                     the ItemsSource specifies what the next nested level's
                     datasource is -->
                <HierarchicalDataTemplate DataType="{x:Type local:Enterprise}"
                                          ItemsSource="{Binding EmpList}">
                    <Label Content="{Binding EntName}"/>
                </HierarchicalDataTemplate>
                <!-- the template for showing the Employee's properties-->
                <DataTemplate DataType="{x:Type local:Employee}">
                    <Label Content="{Binding EmpName}"/>
                </DataTemplate>
            </TreeView.Resources>
        </TreeView>
    </StackPanel>
</Window>


    using System.Collections.ObjectModel;
using System.Windows;

namespace TreeViewSelection
{
    public partial class Window1 : Window
    {
        public ObservableCollection<Enterprise> Enterprises { get; set; }
        public Window1()
        {
            InitializeComponent();
            Enterprises = new ObservableCollection<Enterprise>
                            {
                                new Enterprise("Sweets4Free"),
                                new Enterprise("Tires4Ever")
                            };
            DataContext = this;
        }
    }

    public class Enterprise : DependencyObject
    {
        public string EntName { get; set; }
        public ObservableCollection<Employee> EmpList { get; set; }

        public Enterprise(string name)
        {
            EntName = name;
            EmpList = new ObservableCollection<Employee>
                        {
                            new Employee("John Doe"),
                            new Employee("Sylvia Smith")
                        };
        }
    }
        public class Employee : DependencyObject
        {
            public string EmpName { get; set; }
            public Employee(string name)
            {
                EmpName = name;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)


kiw*_*pom 5

查看Josh Smith 关于这个主题的文章 ......帮助我永无止境!

ps看起来你错过了HierarchicalDataTemplate上的DataType属性,例如

<HierarchicalDataTemplate
    DataType="{x:Type local:ItemType}"
    ItemsSource="{Binding ...}" >
Run Code Online (Sandbox Code Playgroud)