New to Telerik UI for WPF? Download free 30-day trial

DataTrigger Defined in CellTemplate of RadGridView Column Doesn't Work.

Environment

Product Version 2022.3.1109
Product RadGridView for WPF

Description

A DataTrigger defined in a GridViewColumn's CellTemplate or CellEditTemplate of RadGridView doesn't work.

Solution

Several alternative options can be used instead of DataTrigger. The following trigger definition will serve as a reference on how to replace it with another approach.

DataTrigger definition

<telerik:GridViewDataColumn DataMemberBinding="{Binding PersonName}">  
    <telerik:GridViewDataColumn.CellTemplate>  
        <DataTemplate>  
            <TextBlock x:Name="textBlock" Text="{Binding PersonName}" />  
            <DataTemplate.Triggers>  
                <DataTrigger Binding="{Binding IsPersonAvailable}" Value="False" >  
                    <Setter TargetName="textBlock" Property="Background" Value="Red" />  
                </DataTrigger>  
            </DataTemplate.Triggers> 
        </DataTemplate>  
    </telerik:GridViewDataColumn.CellTemplate>  
</telerik:GridViewDataColumn>  
Instead, use one of the following ideas:
  • Using a direct data binding for the property targeted by the trigger.

    In that case an additional property in the row's data model should be defined and updated accordingly.

        <telerik:GridViewDataColumn DataMemberBinding="{Binding PersonName}">  
            <telerik:GridViewDataColumn.CellTemplate>  
                <DataTemplate>  
                    <TextBlock x:Name="textBlock" Text="{Binding PersonName}" Background="{Binding PersonBackground}" />  
                </DataTemplate>  
            </telerik:GridViewDataColumn.CellTemplate>  
        </telerik:GridViewDataColumn>  
    
  • Using an IValueConverter.

    In that case the converter can return red color if the bound boolean value is false, and black (or other) color when true.

        <telerik:GridViewDataColumn DataMemberBinding="{Binding PersonName}">  
            <telerik:GridViewDataColumn.CellTemplate>  
                <DataTemplate>  
                    <TextBlock x:Name="textBlock" Text="{Binding PersonName}" Background="{Binding IsPersonAvailable, Converter={StaticResource MyBooleanToBrushConverter}}" />  
                </DataTemplate>  
            </telerik:GridViewDataColumn.CellTemplate>  
        </telerik:GridViewDataColumn>  
    
  • Using the ContentTemplate of GridViewCell instead of the CellTemplate.

    To utilize this idea, use the CellStyle property of the column.

        <telerik:GridViewDataColumn DataMemberBinding="{Binding PersonName}">  
            <telerik:GridViewDataColumn.CellStyle>  
                <Style TargetType="telerik:GridViewCell"> 
                    <Setter Property="ContentTemplate"> 
                        <Setter.Value> 
                            <DataTemplate> 
                                <TextBlock x:Name="textBlock" Text="{Binding PersonName}" />  
                                <DataTemplate.Triggers>  
                                    <DataTrigger Binding="{Binding IsPersonAvailable}" Value="False" >  
                                        <Setter TargetName="textBlock" Property="Background" Value="Red" />  
                                    </DataTrigger>  
                                </DataTemplate.Triggers> 
                            </DataTemplate> 
                        </Setter.Value> 
                    </Setter> 
                </Style> 
            </telerik:GridViewDataColumn.CellTemplate>  
        </telerik:GridViewDataColumn>  
    
In this article