wpf 樹視圖 mvvm (wpf treeview mvvm)


問題描述

wpf 樹視圖 mvvm (wpf treeview mvvm)

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.

thanks

‑‑‑‑‑

參考解法

方法 1:

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

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

I tried to simulate what you want:

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

方法 2:

Check out Josh Smith's article on exactly this topic... Helped me no end!

p.s. Looks like you're missing the DataType property on the HierarchicalDataTemplate, e.g.

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

(by user55474Dabblernlkiwipom)

參考文件

  1. wpf treeview mvvm (CC BY‑SA 3.0/4.0)

#mvvm #wpf #treeview






相關問題

WPF View 在關閉時將 ViewModel 屬性設置為 null (WPF View sets ViewModel properties to null on closing)

在文本框中正確輸入後啟用按鈕 (Button Enable After Correct Input In TextBox)

WPF說數據項不為空時為空 (WPF says that data item is null when it is not null)

MVVM - 如何將 ViewModel 包裝在 ViewModel 中? (MVVM - How to wrap ViewModel in a ViewModel?)

關於服務參考和 MVVM 模式的幾個一般問題 (A few general questions about Service Reference and MVVM pattern)

WPF MVVM 鏈接視圖 (WPF MVVM Linked Views)

wpf 樹視圖 mvvm (wpf treeview mvvm)

如何在 MVVM Light 的 ListView 中的 ComboBox 中顯示列表? (How to show a List in a ComboBox in a ListView in MVVM Light?)

多次調用 PropertyChanged 的 ViewModel 屬性 (ViewModel properties with multiple calls to PropertyChanged)

如何將圖像存儲在類庫中並從任何類訪問它 (How can i store an image in a class library and access it from any class)

Silverlight MVVM 隔離存儲 (Silverlight MVVM Isolated Storage)

如何將文本框的borderBrush屬性綁定到viewmodel中的屬性,類型轉換錯誤 (How to bind the borderBrush property of a textbox to a property in viewmodel, type conversion error)







留言討論