【问题标题】:Bind object (with list member) to datagrid将对象(带有列表成员)绑定到数据网格
【发布时间】:2023-03-31 08:25:02
【问题描述】:

我有一个问题。我想将对象列表绑定到数据网格。绑定没问题,它正在工作,但我遇到了这些对象中的一个成员的问题:List 类型。

DataGrid 仅在此单元格中显示“(集合)”。这是我的课:

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

    List<double> input;
}

当我将它绑定到我的 dataGrid 时,我得到一个列:

[输入]
(收藏)
(收藏)
(收藏)
(收藏)

谁能帮我正确显示双精度值,每一个都在一列中?

提前谢谢你。

【问题讨论】:

  • 这是ASP.NET吗?另外,它绑定到什么 type 列?文本框列?
  • 欢迎来到 SO。不错的第一篇文章!

标签: c# .net list datagrid


【解决方案1】:

为了将 DataGrid 绑定到集合,必须指定每列应从绑定对象中获取哪些数据。你这样做:

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

对于简单类型,您可以使用这些模板代替DataGridTemplateColumn

  • DataGridCheckBoxColumn 用于布尔值
  • DataGridComboBoxColumn 用于可枚举值
  • DataGridHyperlinkColumn 用于 Uri 值
  • DataGridTextColumn 显示文本值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 2014-06-29
    相关资源
    最近更新 更多