Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Background Color of DataGridCells Changing when Scrolling

$
0
0

All, after a long time working out how to change the background color of cells at run-time, I now have a problem where the correctly colored rows are not staying coloured when I scroll the DataGrid!? I color the rows/cells via:

    public void RunChecks()
    {
        const int baseColumnCount = 3;
        for (int i = baseColumnCount; i < dataGrid.Columns.Count; i++)
        {
            foreach (DataGridRow row in Utilities.GetDataGridRows(dataGrid))
            {
                if (row != null)
                {
                    DataGridCell cell = dataGrid.GetCell(row, i);

                    // Start work with cell.
                    Color color;
                    TextBlock tb = cell.Content as TextBlock;
                    string cellValue = tb.Text;
                    if (!CheckForBalancedParentheses(cellValue))
                        color = (Color)ColorConverter.ConvertFromString("#FF0000");
                    else
                        color = (Color)ColorConverter.ConvertFromString("#FFFFFF");
                    row.Background = new SolidColorBrush(color);
                    //cell.Background = new SolidColorBrush(color);
                }
            }
        }
    }

where I have the following utility methods

    public static T GetVisualChild<T>(Visual parent) where T : Visual
    {
        T child = default(T);
        int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < numVisuals; i++)
        {
            Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
            child = v as T;
            if (child == null)
                child = GetVisualChild<T>(v);
            if (child != null)
                break;
        }
        return child;
    }

    public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int column)
    {
        if (row != null)
        {
            DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
            if (presenter == null)
            {
                grid.ScrollIntoView(row, grid.Columns[column]);
                presenter = GetVisualChild<DataGridCellsPresenter>(row);
            }
            DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            return cell;
        }
        return null;
    }

    public static IEnumerable<DataGridRow> GetDataGridRows(DataGrid grid)
    {
        var itemsSource = grid.ItemsSource as IEnumerable;
        if (null == itemsSource) yield return null;
        foreach (var item in itemsSource)
        {
            var row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
            if (null != row) yield return row;
        }
    }

My question is How do I prevent the correctly colored cells from changing during a scroll etc.?

Thanks for your time.


"Everything should be made as simple as possible, but not simpler" - Einstein


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>