<Grid >
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Name="grid">
<Grid.ColumnDefinitions>
<ColumnDefinition Name="stationGridBcolumn0"/>
<ColumnDefinition Name="stationGridBcolumn1" />
<ColumnDefinition Name="stationGridBcolumn2" />
<ColumnDefinition Name="stationGridBcolumn3" />
<ColumnDefinition Name="stationGridBcolumn4" />
<ColumnDefinition Name="stationGridBcolumn5" />
</Grid.ColumnDefinitions>
<Grid Grid.Column="0" Name="g0">
<Button Name="b0" Content="0"/>
</Grid>
<Grid Grid.Column="1" Name="g1">
<Button Name="b1" Content="1"/>
</Grid>
<Grid Grid.Column="2" Name="g2">
<Button Name="b2" Content="2"/>
</Grid>
<Grid Grid.Column="3" Name="g3">
<Button Name="b3" Content="3"/>
</Grid>
<Grid Grid.Column="4" Name="g4">
<Button Name="b4" Content="4"/>
</Grid>
<Grid Grid.Column="5" Name="g5">
<Button Name="b5" Content="5"/>
</Grid>
</Grid>
<Grid Grid.Row="1" >
<StackPanel>
<Button Content="Remove" Click="Button_Click_1"></Button>
<Button Content="Add" Click="Button_Click_2"></Button>
<Button Content="Insert" Click="Button_Click_3"></Button>
</StackPanel>
</Grid>
</Grid>
/// <summary>
/// 这只是个例子,上一步操作一定是删除操作,Add操作会把删除的列加进来
/// </summary>
/// <param name="info"></param>
public void Add(Info info)
{
ColumnDefinition columnDefinition = info.ColumnDefinition;
UIElement uiElement = info.UIElement;
Grid.SetColumn(uiElement, grid.Children.Count);
grid.ColumnDefinitions.Add(columnDefinition);
grid.Children.Add( uiElement);
}
/// <summary>
/// 这只是个例子,上一步操作一定是删除操作,Insert操作会把删除的列加进来
/// </summary>
/// <param name="info"></param>
/// <param name="colIndex"></param>
public void Insert(Info info, int colIndex)
{
ColumnDefinition columnDefinition = info.ColumnDefinition;
UIElement uiElement = info.UIElement;
Grid.SetColumn(uiElement, colIndex);
grid.ColumnDefinitions.Insert(colIndex, columnDefinition);
grid.Children.Insert(colIndex, uiElement);
int index = 0;
foreach (UIElement child in grid.Children)
{
Grid.SetColumn(child, index);
index++;
}
}
private Info Remove(int colIndex)
{
Info info = new Info(grid.ColumnDefinitions[colIndex], grid.Children[colIndex]);
grid.ColumnDefinitions.RemoveAt(colIndex);
grid.Children.RemoveAt(colIndex);
foreach (UIElement child in grid.Children)
{
int curCol = Grid.GetColumn(child);
if (curCol > colIndex)
{
Grid.SetColumn(child, curCol - 1);
}
}
return info;
}
Info info;
private void Button_Click_1(object sender, RoutedEventArgs e)
{
info = Remove(4);
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
Add(info);
}
private void Button_Click_3(object sender, RoutedEventArgs e)
{
Insert(info,4);
}