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

How to paint cells in Columns with IsReadOnlyBinding grey

4 Answers 48 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Inger Marie
Top achievements
Rank 1
Inger Marie asked on 27 Jan 2016, 08:53 AM

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

Sort by
0
Dilyan Traykov
Telerik team
answered on 28 Jan 2016, 01:38 PM
Hello Inger Marie,

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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Inger Marie
Top achievements
Rank 1
answered on 28 Jan 2016, 01:43 PM

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?

0
Accepted
Dilyan Traykov
Telerik team
answered on 02 Feb 2016, 01:29 PM
Hello Inger Marie,

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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Inger Marie
Top achievements
Rank 1
answered on 03 Feb 2016, 08:55 AM

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);
      }

Because not all my columns are GridViewDataColumns and because I got an exception which I think came when I set IsReadOnly directly on the column. The exception is gone with these changes.
Tags
GridView
Asked by
Inger Marie
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Inger Marie
Top achievements
Rank 1
Share this question
or