wpf 是否可以将包含 List<String> 的 List<Object> 绑定到 DataGrid

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24359271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:58:51  来源:igfitidea点击:

Is it possible to bind a List<Object> which contains List<String> to DataGrid

c#wpfdatagrid

提问by Usual Suspect

My question is how to bind a List<Object>( Which contains a List<String>) to a DataGrid in WPF

我的问题是如何在 WPF 中将一个List<Object>(其中包含一个List<String>)绑定到一个 DataGrid

Lets say the Class looks like this

让我们说班级看起来像这样

Class Student
{
         String Name;
         List<String> Marks;
}

Lets say i have List<Student>but how would i bind this to a Data Grid in the Data Source.

可以说我有,List<Student>但是我如何将它绑定到数据源中的数据网格。

I am sure this, DataGrid.DataSource=List<Student>wouldn't work as expected.

我确信这DataGrid.DataSource=List<Student>不会按预期工作。

This is how i want

这就是我想要的

enter image description here

在此处输入图片说明

采纳答案by pushpraj

here you go

干得好

    <DataGrid ItemsSource="{Binding Students}"
              AutoGenerateColumns="False"
              CanUserAddRows="False">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Name}"
                                Header="Name" />
            <DataGridTextColumn Binding="{Binding Marks[0]}"
                                Header="Mark1" />
            <DataGridTextColumn Binding="{Binding Marks[1]}"
                                Header="Mark2" />
            <DataGridTextColumn Binding="{Binding Marks[2]}"
                                Header="Mark3" />
        </DataGrid.Columns>
    </DataGrid>

also change class as

也将班级更改为

    class Student
    {
        public String Name { get; set; }
        public List<String> Marks { get; set; }
    }

note I made public properties for your variables

注意我为你的变量创建了公共属性

result

结果

sample

样本

Variable Columns

变量列

you can have variable number of columns but not for each row

你可以有可变数量的列,但不是每一行

method1 hard-code maximum columns in xaml, so if a column does not have a value for that row it will remain empty

method1 硬编码 xaml 中的最大列数,因此如果某列没有该行的值,它将保持为空

eg

例如

grid sample 2

网格样本 2

I have added another column to demonstrate clearly

我添加了另一列来清楚地展示

<DataGridTextColumn Binding="{Binding Marks[3]}"
                    Header="Mark4" />

other approach involve to generate columns at run-time via code behind or via help of attached properties

其他方法涉及在运行时通过代码隐藏或通过附加属性的帮助生成列

回答by Dhaval Patel

you have to use below menioned code

你必须使用下面提到的代码

Class Student
{
         String Name;
         List<String> Marks;
}


 private ObservableCollection<Student> _student=new ObservableCollection<Student>();

    public ObservableCollection<Student> student
    {
        get { return _student; }
        set { _student = value; }
    }

and your Itemsource look like

你的 Itemsource 看起来像

<DataGrid ItemsSource="{Binding student}">
        <DataGrid.ItemTemplate>
            <DataTemplate>
                <DataGrid ItemsSource="{Binding Marks}"></DataGrid>
            </DataTemplate>
        </DataGrid.ItemTemplate>
    </DataGrid>

回答by Rang

you can try like this

你可以这样试试

<DataGrid ItemsSource="{Binding lstStu}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="name" Binding="{Binding Name}"/>
                <DataGridTemplateColumn Header="list">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ListBox ItemsSource="{Binding Marks}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

if you don't want to display mark in list and you have fixed number of mark,you can do it like this

如果你不想在列表中显示标记并且你有固定数量的标记,你可以这样做

<DataGridTextColumn Header="name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="mark1" Binding="{Binding Marks[0]}"/>
<DataGridTextColumn Header="mark2" Binding="{Binding Marks[1]}"/>

回答by klm_

In the first place you need to have public properties instead of fields in your model. Like that:

首先,您需要在模型中拥有公共属性而不是字段。像那样:

public class Student
{
         public String Name {get;set;}
         public List<String> Marks {get;set;}
}

Then provide datacontext for the grid. In code behind add something like that:

然后为网格提供数据上下文。在后面的代码中添加类似的东西:

var StudentsList = new List<Student>();
//populate the list.....    
DataContext=StudentsList;

The last thing is to bind the DataContext to controls in xaml. Like in the other answers:

最后一件事是将 DataContext 绑定到 xaml 中的控件。就像在其他答案中一样:

 <ListView ItemsSource="{Binding StudentsList}">
        <ListView.ItemTemplate>
                <DataTemplate>
                       <DataGrid ItemsSource="{Binding Marks}" />
                </DataTemplate>
        </ListView.ItemTemplate>
 </ListView>