简体   繁体   English

如何动态地修改WPF网格的行和列定义?

[英]How can one dynamically modify row and column definitions of a WPF grid?

I have two dimensional data with varying number of rows and columns and must display it to the user for editing. 我有二维数据,其中行和列的数量不同,并且必须将其显示给用户进行编辑。 The format of the data itself is essentially described by a list of row and column descriptors with a header text for each row or column. 数据本身的格式本质上是由行和列描述符的列表描述的,每个行或列的标题文本。

I derived a control from Grid that has two properties for the row and column descriptors and builds the gird rows and columns based on that information. 我从Grid派生了一个控件,该控件具有行和列描述符的两个属性,并基于该信息构建网格行和列。 It also subscribes list change events of the two descriptor collection to update itself if the user dynamically adds or removes rows or columns. 如果用户动态添加或删除行或列,它还订阅两个描述符集合的列表更改事件以更新自身。

And here the problem occurs - when I try to modify the row or column definitions of the gird I get an exception telling that the collection is read-only. 这里出现了问题-当我尝试修改网格的行或列定义时,出现异常,告诉该集合是只读的。 So I assume it is not possible to modify the definition after the grid has been created and shown once. 因此,我假设在创建并显示一次网格之后就无法修改定义。 Any ideas? 有任何想法吗?

And just in the case it matters - everything is data bound. 只是在重要的情况下-一切都是数据绑定的。 There is a (dynamic) collection of tables with each table containing its own (dynamic) row and column definitions and the data entered for each cell. 有一个(动态)表集合,每个表包含其自己的(动态)行和列定义以及为每个单元格输入的数据。

I did a quick test and I could add rows and columns without problems to a Grid at runtime through code, using the RowDefinitions and ColumnDefinitions collections. 我进行了快速测试,可以在运行时通过代码使用RowDefinitions和ColumnDefinitions集合将行和列毫无问题地添加到Grid。 I don't think that bindings have something to do with the problem either. 我也不认为绑定与问题有关。

If on the other hand you meant DataGrid, yes, that changes things quite a bit. 另一方面,如果您的意思是DataGrid,是的,那会改变很多事情。

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

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