简体   繁体   English

WPF DataGrid 将单元格绑定到具有动态列的数据模型

[英]WPF DataGrid binding cell's to data model with dynamic columns

I've got a data model that can be considered as a table with dynamic columns and rows.我有一个数据模型,可以将其视为具有动态列和行的表。

The model exposes a collection that defines the columns that currently exist.该模型公开了一个定义当前存在的列的集合。 Each column is an object (a model) itself.每列都是一个对象(一个模型)本身。 The model also exposes a collection containing the rows.该模型还公开了一个包含行的集合。 Also each row is a model representing the row.每行也是代表行的模型。

Each row exposes a collection that contains the column values.每行公开一个包含列值的集合。 Each column "value" is a model too.每列“值”也是一个模型。

MyModel
    - Columns (list of MyModelColumn)
    - Rows (list of MyModelRow)
    
MyModelColumn
    - Header (string)
    - ...

MyModelRow
    - Values (list of MyModelCell)
    - ...
    
MyModelCell
    - CalculatedValue
    - DisplayValue
    - Color
    - ...

What I would like to achieve is:我想实现的是:

  • A DataGrid that has columns dynamically created, corresponding to MyModel.Columns具有动态创建的列的DataGrid ,对应于MyModel.Columns
  • Each cell in the DataGrid must be able to bind to properties of the corresponding MyModelCell . DataGrid中的每个单元格都必须能够绑定到相应MyModelCell属性。
  • Each DataGrid column is a DataGridTemplateColumn .每个DataGrid列都是一个DataGridTemplateColumn

It seems to be not too hard, but I tried several different approaches and still can't get it done.似乎不太难,但我尝试了几种不同的方法,仍然无法完成。

I managed creating DataGrid columns dynamically (from codebehind).我管理动态创建DataGrid列(从代码隐藏)。

The DataGrid 's own behavior makes each cell in a DataGridTemplateColumn have its DataContext set to the "data item" that belongs to the whole row.DataGrid自己的行为使得在每个单元DataGridTemplateColumn有它DataContext设置为属于整排的‘数据项’。 In this case, the DataContext of each DataGrid cell is a MyModelRow instance.在这种情况下,每个DataGrid单元格的DataContext是一个MyModelRow实例。

The DataTemplate for the DataGrid cell knows about the MyModelRow , and can bind to properties on that class directly, but that's not what I want. DataGrid单元格的DataTemplate知道MyModelRow ,并且可以直接绑定到该类上的属性,但这不是我想要的。 I want to bind to the corresponding MyModelCell that belongs to the row/column.我想绑定到属于行/列的相应MyModelCell From within the CellTemplate (and CellEditingTemplate) you can never determine in what column the cell is, and so you don't know which MyModelCell (from MyModelRow.Values ) you have to access to read a property of the MyModelCell .在 CellTemplate(和 CellEditingTemplate)中,您永远无法确定单元格在哪一列,因此您不知道您必须访问哪个MyModelCell (来自MyModelRow.Values )才能读取MyModelCell的属性。

The main problem is: how to connect each cell in the DataGrid to the MyModelCell it belongs to?主要问题是:如何将DataGrid中的每个单元格连接到它所属的MyModelCell?

My data model classes I'm talking about are viewmodels, and the problem is mainly UI related.我正在谈论的数据模型类是视图模型,问题主要与 UI 相关。 So, using codebehind is fine, and maybe even unavoidable.因此,使用代码隐藏很好,甚至可能是不可避免的。

The main problem is: how to connect each cell in the DataGrid to the MyModelCell it belongs to?主要问题是:如何将DataGrid中的每个单元格连接到它所属的MyModelCell?

Since the DataContext of a DataGridRow is always the current item of type T in the IEnumerable<T> sequence that the DataGrid 's ItemsSource property is set or bound to, you will have to include the index of the cell in the binding paths in the CellTemplate and CellEditingTemplate , eg:由于DataContext一个的DataGridRow总是类型的当前项TIEnumerable<T>序列,所述DataGridItemsSource属性设置或结合,你将不得不包括单元的在所述结合路径的索引CellTemplateCellEditingTemplate ,例如:

Binding="{Binding Values[0].CalculatedValue}"

...for the first column. ...对于第一列。

This means that you will have to (preferably programmatically) create a separate template for each column where you replace [0] in the binding path(s) with the index of that column's cell.这意味着您必须(最好以编程方式)为每一列创建一个单独的模板,在其中将绑定路径中的[0]替换为该列单元格的索引。

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

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