我正在嘗試使用某些條件更改單元格的背景。我正在使用DataGrid.LoadingRow
事件來實現它。但是在向DataGridCellPresenter
投射一行時出現錯誤。該錯誤是將DataGrid行投射到DataGridCellPresenter
非通用方法「System.Windows.FrameworkElement.GetVisualChild(int)的」不能與類型參數」被使用。
下面是錯誤的屏幕截圖
我正在嘗試使用某些條件更改單元格的背景。我正在使用DataGrid.LoadingRow
事件來實現它。但是在向DataGridCellPresenter
投射一行時出現錯誤。該錯誤是將DataGrid行投射到DataGridCellPresenter
非通用方法「System.Windows.FrameworkElement.GetVisualChild(int)的」不能與類型參數」被使用。
下面是錯誤的屏幕截圖
更改單元格背景:
GetCell(dg, row, col).Background = Brushes.Yellow;
public DataGridCell GetCell(DataGrid dg, int row, int column)
{
DataGridRow rowContainer = GetRow(dg, row);
if (rowContainer != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
if (presenter == null)
{
dg.ScrollIntoView(rowContainer, dg.Columns[column]);
presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
}
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
return cell;
}
return null;
}
我假設你正在使用AbZy的代碼。如果是,您可能會忽略另一個函數定義,名爲: public static T GetVisualChild(Visual parent)其中T:Visual
此外GetVisualChild是Visual類中的受保護方法。你不能稱之爲公共。
要更改單元格背景顏色,請參閱[鏈接](http://stackoverflow.com/questions/39543187/setting-datagrid-cell-background-based-on-its-value-c-wpf/39543691#39543691) 。也更好的給你的完整代碼 – Pabdev
在你的錯誤...我通常投與這種格式CastType變量=(CastType)函數(...); – JohnG
根據錯誤,它似乎無法獲得通用的GetVisualChild方法。你是否定義了通用方法? [鏈接](http://stackoverflow.com/questions/14388262/edit-cell-event-getting-cell-value-and-position) –
Pabdev