将列插入表中

发布于 2024-09-09 11:20:08 字数 560 浏览 1 评论 0原文

我有一个包含 3 列的表。现在我想插入另一列,但我想在第一列之后插入新列。现在我想知道如何做到这一点。如果我没记错的话,Table.Columns 属性并不真正代表列,它更多地代表它们的布局,但与列的值无关? 例如,查看 http://msdn.microsoft .com/en-us/library/system.windows.documents.table.columns.aspx

此属性返回的 TableColumn 对象可以与列中的 TableCell 对象结合使用来定义列的布局,但它们不能确定呈现的实际列数。表中的 TableCell 对象决定实际呈现的列数。例如,如果您定义了 3 列,但只有 2 列的表格单元格,则只会呈现 2 列。

所以我想,如果我想添加一个带有值的新列,我必须循环遍历表的行,并且在每一行中我必须在正确的位置添加一个新单元格?

I have a table with 3 columns. Now I want to insert another column, but I want to insert the new column after the first one. Now I'm wondering how this can be done. If I'm not wrong, the Table.Columns property isn't really representing columns, it's more representing their layout but has nothing to do with the values of the columns?
Look e.g. at http://msdn.microsoft.com/en-us/library/system.windows.documents.table.columns.aspx

The TableColumn objects returned by this property can, in conjunction with the TableCell objects in the column, be used to define layout of columns but they do not determine the actual number of columns rendered. It is the TableCell objects in a table that determine how many columns are actually rendered. For example, if you define 3 columns but only have table cells for 2 columns, only 2 columns will be rendered.

So I guess, that if I want to add a new column with values, I have to loop through the rows of the table and in every row I have to add a new cell at the right position?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

⒈起吃苦の倖褔 2024-09-16 11:20:08

您的猜测是正确的,只是需要考虑一些其他的复杂情况。

如果插入位置左侧的任何单元格的 RowSpanColumnSpan 大于 1,则需要对插入位置的单元格索引进行相应调整新细胞。例如,要在第三个可视列位置插入新列,理论上您需要在索引为 2 的现有单元格之前插入新单元格。但是,如果任何行在索引 0 处有一个 RowSpan=2< /code>,该单元格将跨越两列,因此该行中的新单元格必须插入到索引为 1 的单元格之前。

如果跨列单元格跨越了您要插入的列位置,则无需插入新单元格 - 只需增加现有单元格的 ColumnSpan 即可。同样,如果前一行有一个跨行单元格位于插入位置左侧的一列中,您将需要对此进行调整。

事实上,如果要支持全方位的列插入场景,代码会变得相当复杂。您可能希望在私有框架方法 TextRangeEditTables.InsertColumn() 上对您的解决方案进行建模。

Your guess is correct, except that there are some additional complications to consider.

If any of the cells to the left of the insert position have a RowSpan or ColumnSpan greater than 1, you will need to make corresponding adjustments to the cell indexes at which you insert new cells. For example, to insert a new column at the third visual column position, you would theoretically insert new cells before the existing cells with index 2. However, if any of the rows has a cell at index 0 with RowSpan=2, this cell will span two columns, so the new cell in this row must be inserted before the cell with index 1.

If a column-spanning cell crosses the column position that you are inserting into, you do not need to insert a new cell - just increase the the ColumnSpan of the existing cell. Similarly if a previous row has a row-spanning cell that falls in one of the columns to the left of your insertion position, you will need to make adjustments for this.

In fact, if you want to support the full range of column insert scenarios, the code will become quite complex. You might want to model your solution on the private framework method TextRangeEditTables.InsertColumn().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文