I am using a GridView and I have a column with cell merging enabled (IsCellMergingEnabled="True"), also I have a style to set the aligment of the value of the cell:
<Style TargetType="telerik:GridViewMergedCell">
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
I would like to change the background color of the cell based on the value of that cell?
How can I do that?
I was thinking of using a converter but I don't know how can I pass the value of the cell as a parameter.
6 Answers, 1 is accepted
Hello Amige,
Indeed, using the MergedCellsStyleSelector property is the correct approach for setting conditional styling to the merged cells of the control.
I do hope you've managed to create the appropriate selector class, but if you require any further assistance in the process, please let me know.
Regards,
Dilyan Traykov
Progress Telerik
Hi Dilyan,
I was able to create the selector class, now I would like to use that class to style more than one merged column.
I was trying to get the information of each column to set the appropiate style, using:
var cell = container
as
GridViewMergedCell;
var column = cell.DataColumn;
but cell.DataColumn is always null, is there a way to obtain the DataMember or the Name of the columns?
Regards.
Hi Amige,
To obtain the column of the merged cell, you can use the HorizontalStart property of the MergedCellInfo class like so:
public class CustomMergedCellStyleSelector : StyleSelector
{
public override Style SelectStyle(object item, DependencyObject container)
{
var cellInfo = item as MergedCellInfo;
var cell = container as GridViewMergedCell;
var columnIndex = cellInfo.HorizontalStart;
var columns = cell.ParentDataControl.Columns;
if (cellInfo != null && cellInfo.HorizontalStart == columns["Country"].DisplayIndex)
{
return this.CountryColumnStyle;
}
return base.SelectStyle(item, container);
}
public Style CountryColumnStyle { get; set; }
}
For your convenience, I'm attaching a small sample project which demonstrates this approach. Please let me know if you find it helpful.
Regards,
Dilyan Traykov
Progress Telerik
Hi Dilyan,
That is exactly what I was looking for.
Thanks.