首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >wpf treeview mvvm

wpf treeview mvvm
EN

Stack Overflow用户
提问于 2009-12-06 14:24:37
回答 2查看 6.6K关注 0票数 3

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

代码语言:javascript
复制
                <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>

我还有什么遗漏的吗。

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-12-06 18:17:07

嗨,Ian的建议文章确实是一篇很棒的文章!

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

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

代码语言:javascript
复制
<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;
            }
        }
    }
票数 7
EN

Stack Overflow用户

发布于 2009-12-06 16:21:49

看看Josh Smith关于这个话题的article吧。帮了我一个大忙!

附注:看起来您缺少HierarchicalDataTemplate上的DataType属性,例如

代码语言:javascript
复制
<HierarchicalDataTemplate
    DataType="{x:Type local:ItemType}"
    ItemsSource="{Binding ...}" >
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1854541

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档