如何在xaml中删除网格单元格内的元素

时间:2013-06-13 04:56:43

标签: xaml windows-phone-8

我有以下场景(尝试实施棋盘游戏):

我已经声明了一个包含8行和8列的网格。在其中一些中,我通过以下方式以编程方式绘制一些椭圆:

this.myGrid.Children.Add(coin);

此时所有椭圆都画好了。 问题是我无法弄清楚如何删除或隐藏特定列。我试过这些代码行:

this.gridTablero.Children.RemoveAt(1);
this.gridTablero.Children[1].Visibility = System.Windows.Visibility.Collapsed;

但是这些代码行删除了Grid的矩形而不是椭圆。我试着看看我是否可以访问单元格内的椭圆而不是运气: - (

如果有人知道如何做到这一点,那么任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:0)

Children属性与Grid布局无关,此属性是Panel类(Grid类的基类)的一部分,它只包含没有任何信息的子元素集合关于布局。

因此,在您的示例this.gridTablero.Children.RemoveAt(1);中,从Grid中删除第二个(从0开始的索引)子元素。如您所见设置Collapsed只需更改第二个元素的Visibility属性。

这是你可以从第二行删除所有元素的方法:

void HideAllElementsOnRow(int rowIndex)
{
    // Start from end so we can remove elements and continue to enumerate collection
    for (int index = this.gridTablero.Children.Count - 1; index >= 0; index--)
    {
        // If current element located on rowIndex position - we will remove it.
        if (Grid.GetRow(this.gridTablero.Children[index]) == rowIndex)
        {
            this.gridTablero.Children.RemoveAt(index);
        }
    }
}