2016-12-29 49 views
0

我真的很新到C#(1-2天...)。我仍然不知道如何訪問網格,並測試特定行&列中的按鈕是否設置爲屬性「isEnabled」爲true。 我想測試一下是否藍色的按鈕,紅色的是否啓用。C#網格按鈕兒童測試

我不知道如何編寫測試,在本例中訪問@ row [col](或[i] [j]中的@按鈕)和他的屬性「isEnabled」。 這是一個帶按鈕的8 * 8網格。非常感謝幫助我。

public bool isButtonAvaible(Button button) 
    { 
     row = (int)button.GetValue(Grid.RowProperty); 
     col = (int)button.GetValue(Grid.ColumnProperty); 

     foreach (Button b in gridBoard.Children)  //not sure if correct/needed 

     { 
      for (int i = row - 1; i <= row + 1; i++) 
      { 
       for(int j = col - 1; j <= col +1; j++) 
       { 
        if(gridBoard.Children ??? (Button.IsEnabledProperty ==true) 
        { 
         return true; 
        } 
       } 
      } 
     } 
     return false; 
    } 

concept of the test on blue button

回答

0

如果你想運行一個測試,看是否有按鈕被啓用,就像你格式化你的函數,它是那麼容易,因爲

public bool isButtonAvailable(Button btn) 
{ 
    return btn.IsEnabled; 
} 

IsEnabled是布爾屬性,所以如果你要傳遞一個按鈕作爲這個函數的參數,你只想返回你傳遞的按鈕是否啓用的真/假。你嘗試過的其他事情 - 做得太多。

+0

也許函數的名稱並不好。實際上,我嘗試定義一個按鈕周圍的按鈕是否被禁用。如果其中一個被禁用,則按下的按鈕對用戶是可用的。當按鈕堆疊成一個網格時,我想循環進入這個網格,如下所示: –

+0

好的,謝謝Geoff。我找到了。我將編輯答案 –

+0

如果它有幫助,你可以upvote並接受我的答案。謝謝 –

0
private static Button GetChildren(Grid grid, int row, int column) 
    { 
     foreach (Button child in grid.Children) 
     { 
      if (Grid.GetRow(child) == row 
        && 
       Grid.GetColumn(child) == column) 
      { 
       return child; 
      } 
     } 
     return null; 
    } 

但它也可以是更通用:public UIElement GetChildren(etc ...){}