I have a data-bound DataGrid with alternating row background colors. I would like to color a cell differently based on the data it contains. I have tried the solution suggested by this thread
http://wpf.codeplex.com/Thread/View.aspx?ThreadId=51143
But,
DataGridCellsPresenter presenter = GetVisualChild(row)
always returns null.
I am using
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;
}
But VisualTreeHelper.GetChildrenCount() of a DataGridRow always returns 0. I have verified that DataGridRow is not null and has been populated with data already. Any help is appreciated.
Thanks.
First thing first, don't do this in code-behind. You're fighting the framework with this way of doing things. WPF is designed differently; you have to think in terms of how the framework wants you to do things. In the case of WPF, it's XAML markup + converter classes.
You need two things to achieve what you want:
Here goes:
XAML In Your Datagrid
The first thing you want to do is define the XAML necessary to style your DataGrid cells. It looks like this:
What this is doing is setting up a binding to the RelativeSource (the DataGridCell) and telling it to use the Content.Text of the cell as the value to pass to the Converter (dataGridCellConverter).
IValueConverter
The next thing you need is an IValueConverter implementation to actually determine the colors based on the text of the cell:
Here, I'm just looking for the text "CMS" and coloring the background cell; if "CMS" doesn't exist, then it returns the orange color instead.
Specify Resources
Now, you need to add markup in your window/usercontrol to specify the converter as an appropriate resource:
And that should do it! Good luck.
If you know your row and index of the cell you'd like to access, then here's how you can do it in code:
DataGrid Extension: