I have a radgridview, where I use IsReadOnlyBinding on some columns to make specific cells readonly dependent on the dataitem of the row.
However, I would like to give my users a visual clue that they cannot change the values (by setting the background color to grey in the cell style).
I can do this by binding to the same property on the dataitem of the row, but is it possible to do by binding to a property on the GridViewCell?
That way I could make a solution-general cell style in a resouce dictionary and have all my readonly cells go grey.
Thanks!
4 Answers, 1 is accepted
I'm afraid that there's no straightforward way to do this. A simple solution would be to use CellStyleSelector, but you will have to attach them to each separate column you would want to address. Please let me know whether this approach would work for you.
Regards,
Dilyan Traykov
Telerik
Thanks for your reply.
I am not sure how I would go about and have CellStyleSelector react to the value of IsReadOnlyBinding on the column without referring to the underlying dataitem in the CellStyleSelector.
If it can be done for CellStyleSelector, then why not for CellStyle?
I've attached a sample project to show you how the desired behavior can be achieved, using a CellStyleSelector. I've used the CanEdit method of the column to determine if a cell is ReadOnly or not and set a style for it accordingly. This approach is independent of the bound data type, so you should be able to easily reuse it across the whole project.
Please have a look at the provided project, and let me know if this approach works for you. I'm open to any further questions you might have.
Regards,
Dilyan Traykov
Telerik
Thanks! It seems to work.
I am a bit causios because what if I want to use another CellStyle, but just have the background color go grey on readonly. For instance in either case (readonly or not) I would like the cell tooltip to be the text in the cell. Anyway, you gave me a solution I can work with, thanks!
Oh, I changed the StyleSelector to do this:
public override Style SelectStyle(object item, DependencyObject container)
{
GridViewCellBase gridViewCellBase = container as GridViewCellBase;
var col = gridViewCellBase?.Column as GridViewBoundColumnBase;
if (col != null)
{
try
{
return (col.IsReadOnly || !col.CanEdit(item)) ? ReadOnlyStyleProperty : base.SelectStyle(item, container) ;
}
catch (Exception ex)
{
Debug.WriteLine("{0} exception: {1}", nameof(JsGridViewCellStyleSelector), ex);
}
}
return base.SelectStyle(item, container);
}