2011-07-06 4 views
1

두 개의 열과 여러 개의 행이있는 격자가 있습니다. 각 셀에는 많은 컨트롤이 들어 있습니다. 이 컨트롤들 중에는 버튼을 눌렀을 때 현재 그리드 셀의 모든 컨트롤을 삭제해야하는 버튼이 있습니다. 내 단추가있는 표 셀의 인덱스를 가져 오는 방법과이 셀의 모든 컨트롤을 삭제하려면 어떻게합니까?격자 셀 유지 및 그 안에있는 모든 컨트롤 지우기

답변

3

이 기능이 유용합니까? 당신이합니다 (ToList를 발견, System.Linq

//get the row and column of the button that was pressed. 
var row = (int)myButton.GetValue(Grid.RowProperty); 
var col = (int)myButton.GetValue(Grid.ColumnProperty); 

//go through each child in the grid. 
foreach (var uiElement in myGrid.Children) 
{ //if the row and col match, then delete the item. 
    if (uiElement.GetValue(Grid.ColumnProperty) == col && uiElement.GetValue(Grid.RowProperty) == row) 
      myGrid.Children.Remove(uiElement); 
} 
+0

없음을 제거 할 수 있습니다, 그렇지 않습니다 작동하는 것 같습니다. 처음 두 줄은 일을하고 있지만, Linq 부분, 코드의 세 번째 줄이 문제를 일으키는 것 같습니다. 그것이 무엇을하는지 설명 할 수 있다면 그 이유를 알아낼 수 있습니다. 감사! –

+0

죄송합니다. grid.children 및 foreach 자식을 grid.Children에 가져옵니다. 자식이 단추와 같은 행과 열에 있으면 삭제하십시오. 당신은 아마 실제로 그것을 하나의 문장으로 결합 할 수 있습니다. 아쉽게 원본 게시판을 업데이트하십시오. –

+0

예, 귀하의 의견이 정확히 내가 한 일이지만, 실제로 작동하지 않습니다. 첫 번째 자식을 삭제 한 후 두 번째 루프에서 foreach 루프에서 InvalidOperationException을 발생시킵니다. 어떤 생각? 다시 한 번 감사드립니다! –

1

이전 답을 LINQ를 사용 연장에 대한 using 문을 추가해야합니다) 그래서 당신은 즉시 요소

//get the row and column of the button that was pressed. 
var row = (int)myButton.GetValue(Grid.RowProperty); 
var col = (int)myButton.GetValue(Grid.ColumnProperty); 

//go through each child in the grid. 
//if the row and col match, then delete the item. 
foreach (var uiElement in myGrid.Children.Where(uiElement => (int)uiElement.GetValue(Grid.ColumnProperty) == col && (int)uiElement.GetValue(Grid.RowProperty) == row).ToList()) 
{ 
    myGrid.Children.Remove(uiElement); 
} 
관련 문제