Wpf treeview mvvm

I am trying to populate treeview with mvvm, but the tree does not display any data. I have a list of Employee, which is the property of my vm, which contains data about employees. 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 something I donโ€™t see here.

thanks

+3
source share
2 answers

Hi, Jan suggested the article is really wonderful!

The trick is that you should specify how the TreeView displays its elements through type-specific (hierarchical) DataTemplates. You specify this data in Treeview resources (or above the visual tree if you want to reuse them in more tree structures).

, :

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

Josh Smith ... !

p.s. , DataType HierarchicalDataTemplate, .

<HierarchicalDataTemplate
    DataType="{x:Type local:ItemType}"
    ItemsSource="{Binding ...}" >
+5

Source: https://habr.com/ru/post/1724845/


All Articles