简体   繁体   English

将对象(带有列表成员)绑定到datagrid

[英]Bind object (with list member) to datagrid

I've got a problem. 我有一个问题。 I want to bind an List of Objects to a datagrid. 我想将对象列表绑定到数据网格。 The binding is no problem, it's working but I've got a problem with one member of these objects: a List type. 绑定是没有问题的,它正在工作,但是这些对象的一个​​成员存在问题:列表类型。

The DataGrid only shows "(Collection)" in this cell. DataGrid在此单元格中仅显示“(集合)”。 Here is my class: 这是我的课:

public class ObjectOfMyProgram
{
    double val1;
    double val2;
    double result;

    List<double> input;
}

When I bind it to my dataGrid I get a column: 当我将其绑定到我的dataGrid时,我得到一列:

[Input] [输入]
(Collection) (采集)
(Collection) (采集)
(Collection) (采集)
(Collection) (采集)

Can anyone help me to show the double values correctly, each in one column? 谁能帮助我在一个栏中正确显示双精度值?

Thank you in advance. 先感谢您。

In order to bind a DataGrid to a collection it is necessary to specify which data each column should take from the bound object. 为了将DataGrid绑定到集合,必须指定每列应从绑定对象中获取哪些数据。 You do it this way: 您可以这样进行:

<DataGrid ItemsSource="{Binding collection}" AutoGenerateColumns="False" >
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="val1">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding val1}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
         </DataGridTemplateColumn>
         <!-- add more columns here -->
    </DataGrid.Columns>
</DataGrid>

For simple types you can use these templates instead of DataGridTemplateColumn : 对于简单类型,可以使用以下模板代替DataGridTemplateColumn

  • DataGridCheckBoxColumn for boolean values 用于布尔值的DataGridCheckBoxColumn
  • DataGridComboBoxColumn for enumerable values DataGridComboBoxColumn用于可枚举的值
  • DataGridHyperlinkColumn for Uri values Uri值的DataGridHyperlinkColumn
  • DataGridTextColumn to show text values DataGridTextColumn显示文本值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM