How to paint cells in Columns with IsReadOnlyBinding grey

5 posts, 1 answers
  1. Inger Marie
    Inger Marie avatar
    60 posts
    Member since:
    Nov 2010

    Posted 27 Jan Link to this post

    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!

  2. Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    368 posts

    Posted 28 Jan Link to this post

    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
  3. UI for WPF is Visual Studio 2017 Ready
  4. Inger Marie
    Inger Marie avatar
    60 posts
    Member since:
    Nov 2010

    Posted 28 Jan in reply to Dilyan Traykov Link to this post

    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?

  5. Answer
    Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    368 posts

    Posted 02 Feb Link to this post

    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
  6. Inger Marie
    Inger Marie avatar
    60 posts
    Member since:
    Nov 2010

    Posted 03 Feb Link to this post

    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.
Back to Top
UI for WPF is Visual Studio 2017 Ready