如何将单元格中的用户控件替换为Grid。 WPF?

时间:2012-01-07 16:40:00

标签: c# wpf user-controls grid

我有一个包含5行和5列的Grid。我动态创建了这个Grid。每个单元格都包含自定义用户控我想用给定行和列的其他用户控件动态替换用户控件。您可以看到创建Grid here的方法实现。

我的问题是如何在已经创建Grid之后替换给定行和列的用户控件?对不起,如果我的英语不好!

提前谢谢!

1 个答案:

答案 0 :(得分:1)

此功能可能符合您的需求

public void ReplaceItemAt(Grid grid, FrameworkElement fe, int row, int col)
{
    // clear desired cell
    var items = grid.Children
        .Where(x => Grid.GetRow(x) == row && Grid.GetColumn(x) == col)
        .ToArray();
    foreach(var item in items) grid.Children.Remove(item);

    // make sure the new item is positioned correctly
    Grid.SetRow(fe, row);
    Grid.SetColumn(fe, col);

    // insert the new item into the grid
    grid.Children.Add(fe);
}
相关问题