wpf 树状视图 mvvm [英] wpf treeview mvvm

查看:55
本文介绍了wpf 树状视图 mvvm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to populate a treeview using mvvm but the tree does not display any data. I have a Employee list which is a property in my vm which contains he employee data. the xaml is as follows.

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

这里有什么我遗漏的吗.

Is there anything i am missing here.

谢谢

推荐答案

嗨 Ian 推荐的文章确实值得一读!

Hi Ian's suggested article indeed is a great read!

诀窍是您应该通过特定于类型的(分层)DataTemplates 指定 Treeview 如何显示其项目.您可以在 Treeview 的资源中指定这些数据模板(或者,如果您想在更多的树视图中重用它们,可以在可视化树的更高位置).

The trick is that you should specify how the Treeview shows its items through type specific (Hierarchical)DataTemplates. You specify these datatemplates in the Treeview's resources (or higher up the visual tree if you want to reuse them in more treeviews).

我试图模拟你想要的:

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

这篇关于wpf 树状视图 mvvm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆