WPF 将 List 绑定到 TreeView 和分组 [英] WPF bind List to TreeView and grouping

查看:60
本文介绍了WPF 将 List 绑定到 TreeView 和分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 ViewModel 中有列表.我想根据属性对这个列表对象进行分组,并在 Treeview 中显示它.

I have list in my ViewModel. I want to group this list objects based on property and show it in Treeview.

class ProductsViewModel
{
    public List<Product> ProductList;
}

class Product
{
    public string Name;
    public string Category;
    public double Price;
}

在我的 WPF 窗口中,我需要根据产品的类别在 TreeView 中的 ProductList 中显示产品.请指导我.

In my WPF window I need to show the products in ProductList in TreeView based on the category of the product. Please guide me.

Category 1
    P1
    P2
Category 2
    P3
    P4
    P5

推荐答案

Change Product &ProductViewModel 类与此:

Change Product & ProductViewModel class with this :

class ProductsViewModel
{
    private List<Product> productList;

    public List<Product> ProductList
    {
        get 
        { return productList; }
        set 
        { productList = value; }
    }
}

class Product
{
    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    private string category;

    public string Category
    {
        get { return category; }
        set { category = value; }
    }

    private double price;

    public double Price
    {
        get { return price; }
        set { price = value; }
    }
}

主窗口.xaml

<Window x:Class="WpfApplication11.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TreeView x:Name="treeView" HorizontalAlignment="Left" Height="178" Margin="85,24,0,0" VerticalAlignment="Top" Width="279"/>

    </Grid>
</Window>

主窗口构造函数:

    public MainWindow()
    {
        ProductsViewModel viewModel = new ProductsViewModel();
        viewModel.ProductList = new List<Product>();
        viewModel.ProductList.Add(new Product() { Name = "ABC", Category ="1", Price = 100 });
        viewModel.ProductList.Add(new Product() { Name = "PQR", Category = "2", Price = 100 });
        viewModel.ProductList.Add(new Product() { Name = "ABC", Category = "3", Price = 100 });
        viewModel.ProductList.Add(new Product() { Name = "XYZ", Category = "2", Price = 100 });

        InitializeComponent();

        var GetCategories = viewModel.ProductList.Select(p => p.Category).Distinct();
        foreach (var item in GetCategories)
        {
            TreeViewItem node = new TreeViewItem() { Header = item };
            treeView.Items.Add(node);
            var products = viewModel.ProductList.Where(p => p.Category == item).Select(p => p.Name).Distinct();
            foreach (var product in products)
            {
                node.Items.Add(product);
            }
        }
    }

这篇关于WPF 将 List 绑定到 TreeView 和分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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