Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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);
                    }
                }
            }
        }



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



C#
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
Updated 11-Jan-18 7:21am

 
Share this answer
 
Comments
rohith naik 29-May-13 10:02am    
Please help me with the problem I have. Your link is almost completely irrelevant.
Kenneth Haugland 29-May-13 10:29am    
I dont actually think so, with a IValueConverter I dont think your problem will occur.
rohith naik 30-May-13 7:07am    
Can you atleast tell me why you think using a IValueConverter will help my problem ?
Graeme_Grant 12-Jan-18 3:55am    
WPF controls work best if you use Data Binding.

IValueConverter or IMultiValueConverter use the value passed and outputs a modified value using data binding. So you can pass any value to the converter and output the calculated value for that property.
rohith naik 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 code will help you.

C#
private void GetCell(DataGridRow row, DataGridColumn column)
{

    DependencyObject parent = row;

    Lable1:

    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
    {
        var item = VisualTreeHelper.GetChild(parent, i);
        if (item is DataGridCellsPresenter)
        {
            DataGridCellsPresenter presenter = item as DataGridCellsPresenter;

            DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(1) as DataGridCell;

            cell.Background = Brushes.Red;

            break;
        }
        else
        {
            parent = item;
            goto Lable1;
        }
    }
}
 
Share this answer
 
Comments
CHill60 12-Jan-18 9:07am    
Only 4 years late

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900