2013-08-22 2 views
0

표시된 셀의 색을 변경하려면 DataGrid의 일부 셀을 표시하고 싶습니다. 나는 하나의 셀에 대해 그들과 코드를이 작업을 수행 할 수 있습니다표시된 셀의 색 변경

public static DataGridRow GetRow(this DataGrid dataGrid, int index) 
    { 
     DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index); 
     if (row == null) 
     { 
      dataGrid.UpdateLayout(); 
      dataGrid.ScrollIntoView(dataGrid.Items[index]); 
      row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index); 
     } 
     return row; 
    } 

    public static int GetRowIdx(this DataGrid dataGrid, DataGridCellInfo cellInfo) 
    { 
     // Use reflection to get DataGridCell.RowDataItem property value. 
     DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(cellInfo.Item); 
     if (row == null) 
      throw new NullReferenceException("Fehler: Keine Index gefunden da DataGridRow null!"); 
     return row.GetIndex(); 
    } 



    public static DataGridCell GetCurrentCell(this DataGrid dataGrid) 
    { 
     int row = GetRowIdx(dataGrid, dataGrid.CurrentCell); 
     int column = dataGrid.CurrentColumn.DisplayIndex; 

     return GetCell(dataGrid, row, column); 
    } 

호출 :

DataGridCell currentCell = DataGridExtension.GetCurrentCell(dataGrid1); 
    currentCell.Background = Brushes.LightGray; 

사람이 코드를 변경하는 방법을 알고를, 내가 예를 들어 5 개 세포를 표시하고 자신의 색상을 변경할 수 있도록 ?

당신은 DataGridCell의 컬렉션을 만들고 버튼을 클릭하는 것과 같은, 다른 이벤트에 모두 표시 할 수

답변

1

: 다음

List<DataGridCell> CellList = new List<DataGridCell>(); 

해당 이벤트가 셀리스트에 셀을 추가 할 셀을 클릭 할 때마다 :

를 그런 다음
DataGridCell currentCell = DataGridExtension.GetCurrentCell(dataGrid1); 
CellList.Add(currentCell); 

당신이 새로운 색상으로 모든 셀을 변경 버튼을 클릭 이벤트 처리기에 이것을 추가 할 :

foreach (DataGridCell cell in CellList) 
{ 
    cell.Background = Brushes.LightGray; 
} 
관련 문제