Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF DataGrid
I am using the below method to highlight cells from my code-behind for my data grid. But the problem is that when I scroll out of view, the highlighted cells are getting lost and are jumping around. I have set both row and column virtualization to false. please help.
 
public void ColourCell(int row, int column, Color color, bool HighlightFlag = true)
        {
            DataGridRow rowContainer = GetRow(row);
 
            if (rowContainer != null)
            {
                DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
                // try to get the cell but it may possibly be virtualized but enable virtualizion is off
                DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                // now try to bring into view and retreive the cell
                if (cell != null)
                {
                    if (HighlightFlag == false)
                        cell.Background = new SolidColorBrush(Colors.MediumPurple);
                    else
                    {
                        cell.Background = new SolidColorBrush(color);
                    }
                }
            }
        }
 

public DataGridRow GetRow(int index)
        {
            DataGridRow row = (DataGridRow)DataGridControl.ItemContainerGenerator.ContainerFromIndex(index);
            if (row == null)
            {
                row = (DataGridRow)DataGridControl.ItemContainerGenerator.ContainerFromIndex(index);
            }
            return row;
        }
 

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;
        }
Posted 29-May-13 4:12am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
rohith naik at 29-May-13 10:02am
   
Please help me with the problem I have. Your link is almost completely irrelevant.
Kenneth Haugland at 29-May-13 10:29am
   
I dont actually think so, with a IValueConverter I dont think your problem will occur.
rohith naik at 30-May-13 7:07am
   
Can you atleast tell me why you think using a IValueConverter will help my problem ?
rohith naik at 30-May-13 7:34am
   
Additionally, I am not using XAML as there is a very complex logic to which cells I want to highlight. This works fine if the datagrid fits into 'one page' of sorts, i.e, there is no need to scroll. But if i scroll down and lose sight of the cell, The background change which I made jumps around.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 515
1 Maciej Los 349
2 Richard MacCutchan 220
3 BillWoodruff 209
4 Mathew Soji 160
0 OriginalGriff 8,654
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,229
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 29 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100