You can use a CellStyleSelector
for this task. The CellStyleSelector will let you determine a particular background color depending on the model's values. In fact, the example code in our documentation for CellStyleSelector
is exactly what you're trying to do.
However, since you're using AutoGenerateColumns=True, you don't have direct access to the column definition in the markup. It's necessary to set the CellStyleSelector on a column.
Therefore you must hook into the GridView's AutoGeneratingColumn event
in order to catch when the column is created for the field and then assign the CellStyleSelector.
Here's an example event handler:
sender, GridViewAutoGeneratingColumnEventArgs e)
&& e.Column.Name ==
Since you don't want this to happen in the code behind of the page, you could use an EventToCommand
behavior. The important part here is that you set PassEventArgsToCommand="True"
Here is an example of setting the behavior and the command in the view model:
and in the ViewModel, define the command:
DelegateCommand GeneratingColumnsCommand => generatingColumnsCommand
?? (generatingColumnsCommand =
var e = args
&& e.Column.UniqueName ==
e.Column.CellStyleSelector = Application.Current.Resources[
Notice that the same event args that you'd use for a code-behind event handler are available in the command's method.
This also means you have full design-time support, here is a screenshot of the demo app in the designer
. The cells are properly colored because of the EventToCommand behavior running the command..
I've attached my demo app for the MVVM approach
. In order to run the demo you'll need to restore the Telerik DLLs, you can do this with the Upgrade Wizard or manually fixing the references.
Thank you for contacting Support and for choosing Telerik by Progress.
Lance | Tech Support Engineer, Sr.
Telerik by Progress
Do you need help with upgrading your WPF project? Try the Telerik API Analyzer
and share your thoughts!