2013-06-13 28 views
1

刪除網格單元內的元素我有下面的情況下(試圖執行一個檢查遊戲):如何在XAML

我已經宣佈,8行8列的網格。在其中一些我以下列方式編程畫幾個省略號:

this.myGrid.Children.Add(coin); 

此時所有的橢圓被漆成確定。 問題是,我無法弄清楚如何刪除或放置一個特定的列不可見。我試過這些代碼行:

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

但是這些代碼行刪除了網格的矩形而不是橢圓。我曾試着看能不能進入細胞內的橢圓形,但不是運氣:-(

如果有人知道如何做到這一點,任何幫助將是非常讚賞。

+0

我不知道我遵循的聲明:「我無法弄清楚如何刪除或把無形的特定列」。你想隱藏列中的硬幣嗎? –

回答

0

Children屬性是無關的網格佈局,這個屬性是(對於Grid類的基類)Panel類的一部分,它只是包含的子元素的集合,而不有關佈線的任何信息。在你的榜樣

所以this.gridTablero.Children.RemoveAt(1);刪除第二個(0爲基礎的指標)的子元素從Grid。設置Collapsed,因爲你已經看到只是改變第二元素的210屬性。

這是如何從第二行刪除所有元素:

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); 
     } 
    } 
} 
+0

謝謝,它爲我工作;-) – MikePR