简体   繁体   English

如何通过后面的代码在另一列的前面添加列?

[英]How to Add Column infront of another column through Code Behind?

While I am in XAML, say I have 2 Column Definitions: 当我使用XAML时,说我有2列定义:

<Grid.ColumnDefinition>
    <ColumnDefinition>
    <ColumnDefinition>
</Grid.ColumnDefinition>

I would like to add an Extra Column at 0th Position in Code behind. 我想在代码后面的第0个位置添加一个额外的列。 But When I add column it automatically embed as the 2nd Positon Column. 但是,当我添加列时,它会自动嵌入为第二个Positon列。

You need to get hold of your grid and than insert into the columndefinitions rather than 'Add'. 您需要掌握自己的网格,而不是将其插入columndefinitions中,而不是“ Add”中。

 Grid layoutRoot = new Grid();
 layoutRoot.ColumnDefinitions.Insert(0, new ColumnDefinition() { Width = new GridLength(0, GridUnitType.Star) });

I think you are coding like following which I think is correct. 我认为您正在编码,但我认为是正确的。

Grid1.ColumnDefinitions.Insert(0, new ColumnDefinition());

There are one more thing you need to do is to plus one to the children in the grid like this, or else, the old items will be put in the first column; 您还需要做一件事,就是像这样向网格中的孩子添加一个,否则旧项目将放在第一列中; so the effect is like what you saw just now. 因此效果就像您刚才看到的一样。

foreach (UIElement item in Grid1.Children)
{
    var columnIndex = Grid.GetColumn(item);
    Grid.SetColumn(item, columnIndex + 1);
}

Here is the simple way to achieve this. 这是实现此目的的简单方法。

Maingrid.ColumnDefinitions.Insert(0, new ColumnDefinition()); Maingrid.ColumnDefinitions.Insert(0,new ColumnDefinition());

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

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