【问题标题】:WPF Datagrid not showing values only rowsWPF Datagrid不显示值仅行
【发布时间】:2018-11-29 12:13:50
【问题描述】:

我需要实现更新 DataGrid,它只有一行和标题,但是可变数量的列(从不同的值中获取数据)和单元格中的值必须能够改变(尽管只能通过程序,而不是用户)。因此,我将 ItemsSource 设置为

ObservableCollection<ObservableCollection<string>>

并希望最好。数据网格生成,显示由

生成的列
DataGridTextColumn newColumn = new DataGridTextColumn { Header = header, Width = column.Width }; 
Aggreg.Columns.Add(newColumn);

生成的数据网格为:

<wpftoolkit:DataGrid x:Name="Aggreg" RowHeaderWidth="0" HeadersVisibility="Column" GridLinesVisibility="None" IsReadOnly="True" ItemsSource="{Binding}" AutoGenerateColumns="False">

然后我将集合绑定到数据网格:

Aggreg.ItemsSource = collection;

当我尝试该程序时,无论集合中有什么数据,都只显示空白行

编辑:只是补充一下 - 问题主要是将数据列绑定到集合中的位置(索引),即第 1 列应该在列表中显示索引为 1 等的项目。

【问题讨论】:

  • 您的收藏是财产吗?你设置 DataContext 吗?此外,您需要绑定 DataGridTextColumn。
  • 如果您使用AutoGenerateColumns="False",则需要将每一列绑定到对应的数据。
  • collection 是 ObservableCollection。我尝试设置 DataContext,仍然以相同的方式结束。我可以将列绑定到 Observable 集合中的位置吗?
  • 如果我打开自动生成,我只会得到一个名为 Count 的列,其中包含嵌套集合中的项目数
  • 所以如果我需要将列绑定到相应的数据,我可以将列绑定到List[index]

标签: c# wpf datagrid


【解决方案1】:

正如 Miamy 所说,需要对 DataGridTextColumns 进行绑定,但我之前的所有绑定尝试都失败了。但是 - 这是以编程方式绑定到 Collection 中的索引的正确语法:

Binding binding = new Binding("NewList[INDEX]");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-16
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    相关资源
    最近更新 更多