如何显示 List<int>在 DataGrid 水平? [英] How to display List&lt;int&gt; in DataGrid horizontally?

查看:28
本文介绍了如何显示 List<int>在 DataGrid 水平?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 DataGrid 中水平显示 List 的每个元素.

I'd like to display each element of List<int> in DataGrid horizontally.

我的数据如下所示:

result.Add(new CatModel(1, "AA", new List<int> { 1, 2, 3 }));
result.Add(new CatModel(2, "BB", new List<int> { 4, 5, 6 }));
result.Add(new CatModel(3, "CC", new List<int> { 7, 8, 9 }));

List 的元素应该分成三个部分,每个元素应该水平显示在 DataGrid 单元格中:

The elements of List<int> should be split into three parts and each element should be shown in a DataGrid cell horizontally:

1    AA    1    2    3
2    BB    4    5    6
3    CC    7    8    9

现在它只显示(Collection).我想我需要研究 XAML,但我找不到任何示例.

Now it shows only (Collection). I think I need to work on the XAML, but I cannot find any example.

这个问题和我的很相似,但解决方案与我想要的不同.我更愿意将列分成三部分并将它们分配给一个单元格.

This question is similar to mine, but the solution is different from what I want. I'd rather like to split the column into three parts and allocate each of them to a cell.

顺便说一下,引入List的目的是显示任意数量的元素.您可以假设所有行的长度完全相同,例如,如果第一行有 7 个元素,则所有其他行(这次是第 2 行和第 3 行)也有 7 个元素.

Incidentally, the goal of introducing List<int> is to display arbitrarily many number of elements. You can assume that all rows have exactly the same length, e.g., if the 1st row has 7 elements, all the other rows (this time, the 2nd and the 3rd) have also 7 elements.

这是我的代码:

CatModel.cs

CatModel.cs

using System;
using System.Collections.Generic;

namespace WpfApp1
{
    public class CatModel
    {
        //public CatModel(int Num, String Name, int Test_0001, int Test_0002, int Test_0003)
        public CatModel(int Num, String Name, List<int> Test_List)
        {
            this.Num = Num;
            this.Name = Name;
            //this.Test_0001 = Test_0001;
            //this.Test_0002 = Test_0002;
            //this.Test_0003 = Test_0003;
            this.Test_List = Test_List;
        }
        public int Num { get; set; }
        public String Name { get; set; }
        //public int Test_0001 { get; set; }
        //public int Test_0002 { get; set; }
        //public int Test_0003 { get; set; }
        public List<int> Test_List { get; set; }
    }
}

MainWindow.xaml.cs

MainWindow.xaml.cs

using System.Collections.Generic;
using System.Windows;

namespace WpfApp1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            List<CatModel> result = new List<CatModel>();

            //result.Add(new CatModel(1, "AA", 1, 2, 3));
            //result.Add(new CatModel(2, "BB", 4, 5, 6));
            //result.Add(new CatModel(3, "CC", 7, 8, 9));

            result.Add(new CatModel(1, "AA", new List<int> { 1, 2, 3 }));
            result.Add(new CatModel(2, "BB", new List<int> { 4, 5, 6 }));
            result.Add(new CatModel(3, "CC", new List<int> { 7, 8, 9 }));

            this.dataGrid.ItemsSource = result;
        }
    }
}

主窗口.xaml

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="List" Height="350" Width="750"
        BorderThickness="1">

    <Grid Width="700" Height="300">
        <DataGrid AutoGenerateColumns="False"  Name="dataGrid" HorizontalAlignment="Left" Margin="10,10,10,10">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Num}" ClipboardContentBinding="{x:Null}" Header="Num" IsReadOnly="True" Width="50"/>
                <DataGridTextColumn Binding="{Binding Name}" ClipboardContentBinding="{x:Null}" Header="Name" IsReadOnly="True" Width="100"/>
                <DataGridTextColumn Binding="{Binding Test_0001}" ClipboardContentBinding="{x:Null}" Header="Test_0001" IsReadOnly="True" Width="*"/>
                <DataGridTextColumn Binding="{Binding Test_0002}" ClipboardContentBinding="{x:Null}" Header="Test_0002" IsReadOnly="True" Width="*"/>
                <DataGridTextColumn Binding="{Binding Test_0003}" ClipboardContentBinding="{x:Null}" Header="Test_0003" IsReadOnly="True" Width="*"/>
                <DataGridTextColumn Binding="{Binding Test_List}" ClipboardContentBinding="{x:Null}" Header="Test_List" IsReadOnly="True" Width="*"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

推荐答案

如果由于某种原因您不能使用 DataTable(如您在评论中所写),那么您将不得不使用编程(在代码中)创建列.

If for some reason you cannot use the DataTable (as you wrote in your comment), then you will have to use programmatic (in code behind) creation of columns.

    public MainWindow()
    {
        InitializeComponent();

        List<CatModel> result = new List<CatModel>();
        result.Add(new CatModel(1, "AA", new List<int> { 1, 2, 3 }));
        result.Add(new CatModel(2, "BB", new List<int> { 4, 5, 6 }));
        result.Add(new CatModel(3, "CC", new List<int> { 7, 8, 9 }));


        dataGrid.Columns.Clear();
        dataGrid.AutoGenerateColumns = false;

        dataGrid.Columns.Add(new DataGridTextColumn() { Header = nameof(CatModel.Num), Binding = new Binding(nameof(CatModel.Num)) });
        dataGrid.Columns.Add(new DataGridTextColumn() { Header = nameof(CatModel.Name), Binding = new Binding(nameof(CatModel.Name)) });

        for (int i = 0; i < 3; i++)
        {
            string path = $"{nameof(CatModel.List)}[{i}]";
            dataGrid.Columns.Add(new DataGridTextColumn() { Header = path, Binding = new Binding(path) });
        }

        dataGrid.ItemsSource = result;
    }

    public class CatModel
    {
        public CatModel(int num, string name, List<int> list)
        {
            Num = num;
            Name = name;
            List = list;
        }

        public int Num { get; }
        public string Name { get; }
        public List<int> List { get; }
    }

还请记住,如果您可以更改CatModel.List"集合(添加/删除/插入/替换其中的元素),则应将其替换为 ObservableCollection.
否则,不会显示集合项的新值.

Also keep in mind that if you can change the "CatModel.List" collection (add/remove/insert/replace elements in it), then it should be replaced with ObservableCollection.
Otherwise, the new values of the collection items will not be displayed.

这篇关于如何显示 List<int>在 DataGrid 水平?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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