0

I am trying to change the background of cell using some condition. I am using the DataGrid.LoadingRow event to achieve it. But I get an error when casting a row to DataGridCellPresenter. The error is

The non-generic method 'System.Windows.FrameworkElement.GetVisualChild(int)' cannot be used with type arguments".

Below is a screenshot of the error

Please click to show Error Image

Sentry
  • 4,102
  • 2
  • 30
  • 38
  • To change the cell background color refer [link](http://stackoverflow.com/questions/39543187/setting-datagrid-cell-background-based-on-its-value-c-wpf/39543691#39543691). Also its better give your complete code – Pabdev Sep 28 '16 at 06:16
  • On your error... I usually cast with this format CastType variable = (CastType)function(...); – JohnG Sep 28 '16 at 06:34
  • As per the error it seems like its not able to get the generic GetVisualChild method. Did you defined the generic method? [Link](http://stackoverflow.com/questions/14388262/edit-cell-event-getting-cell-value-and-position) – Pabdev Sep 28 '16 at 06:35

2 Answers2

0

Changing cell background:

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;
    }
Rom
  • 1,183
  • 1
  • 8
  • 18
0

I assume you are using AbZy's code. If yes, you are probably left out another function definition called: public static T GetVisualChild(Visual parent) where T : Visual

Besides GetVisualChild is a protected method in Visual Class. You can't call it as public.

Community
  • 1
  • 1