This is a migrated thread and some comments may be shown as answers.

Row Color Priority

3 Answers 189 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Levi
Top achievements
Rank 1
Levi asked on 30 Jan 2018, 04:47 PM

Hello,

I have a project in which I have a radgridview that needs to be styled with different colors based on the values that are in the cell. So each cell can have different colors depending on its value. To do this I have just been programmatically changing the cell color from the cell loaded event. This is working fine for me but coloring the cells is having a weird effect that I would like to remove if possible. It appears that the cell colors are rendered on top of the row color and the selected row highlight when you mouse over a row. Is there a way I can easily reverse this so that the row color overrides the cell color while the row highlight is on top of the row color?

 

Thanks

3 Answers, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 02 Feb 2018, 11:45 AM
Hello Levi,

The suggested approach in this scenario is to use the CellStyleSelector property. We do not recommend working directly with the visual elements (such as GridViewCell) as this may cause undesired results when working with RadGridView's UI virtualization.

As for overriding the background color when the current row is highlighted, you have two options:

1) Set a DataTrigger in the cell's ControlTemplate, like so:

<ControlTemplate.Triggers>
    <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource AncestorType=telerik:GridViewRow}}" Value="True">
        <Setter Property="Visibility" Value="Visible" TargetName="Background_Over" />
    </DataTrigger>
</ControlTemplate.Triggers>

2) Set a DataTrigger in the cell's Style used by the style selector, like so:

<Style.Triggers>
    <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource AncestorType=telerik:GridViewRow}}" Value="True">
        <Setter Property="Background" Value="{StaticResource ItemBackground_Over}" />
    </DataTrigger>
</Style.Triggers>

Please note that the ItemBackground_Over is a resource used in the Office_Black theme and if you're using another theme you will need to modify it accordingly.

I hope you find all of this helpful. If any further questions or concerns arise, please don't hesitate to express them.

Regards,
Dilyan Traykov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Levi
Top achievements
Rank 1
answered on 02 Feb 2018, 01:48 PM

Hello Dilyan,

Thank you for the reply. I would like to try the CellStyleSelector but I can't find any examples of it done in the code. My columns for the radgridview are generated dynamically at runtime based on user data so I cannot do this in xaml. Is there any examples you could provide me so I could try the cell style selector?

Thanks

0
Dilyan Traykov
Telerik team
answered on 02 Feb 2018, 03:24 PM
Hello Levi,

I've attached a small sample project to my reply based on the one provided by my colleague in this forum thread. What I've done is to set the CellStyleSelector of the automatically generated column in the AutoGeneratingColumn event. Here's the code of interest:

private void ClubsGrid_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
{
    if (e.ItemPropertyInfo.Name == "StadiumCapacity")
    {
        e.Column.CellStyleSelector = Resources["CapacityStyle"] as StyleSelector;
 
        // or you can create the style selector entirely in code as demonstrated below
 
        //var selector = new NegativeNumberStyle();
        //var negativeStyle = new Style(typeof(GridViewCell));
        //negativeStyle.Setters.Add(new Setter(BackgroundProperty, Brushes.Red));
        //selector.NegativeStyle = negativeStyle;
        //e.Column.CellStyleSelector = selector;
    }
}

I hope you find this example helpful. Please let me know if you need any further assistance on the matter.

Regards,
Dilyan Traykov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
GridView
Asked by
Levi
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Levi
Top achievements
Rank 1
Share this question
or