ReadOnly attribute

10 posts, 0 answers
  1. Valery
    Valery avatar
    32 posts
    Member since:
    May 2011

    Posted 03 Jan Link to this post

    In winforms, this attribute is shown in gray.

    How to see it in a radpropertygrid?

  2. Dess
    Admin
    Dess avatar
    2576 posts

    Posted 04 Jan Link to this post

    Hello, Valery, 

    Thank you for writing.  

    According to the provided brief information, I am not sure what is the exact requirement that you are trying to achieve. The ReadOnly property of the SelectedObject will be shown if the property is browsable. Could you please give us more details about the specific goal that you are trying to accomplish? Thus, we would be able to think about a suitable solution and assist you further. 



    I am looking forward to your reply.

    Regards,
    Dess
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  3. Valery
    Valery avatar
    32 posts
    Member since:
    May 2011

    Posted 04 Jan in reply to Dess Link to this post

    If for the item

    ReadOnlyAttribute = true, 

    then propertygrid draws this item gray.

    But radpropertygrid draws this theme BLACK

  4. Valery
    Valery avatar
    32 posts
    Member since:
    May 2011

    Posted 04 Jan in reply to Dess Link to this post

    Example

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                this.radPropertyGrid1.SelectedObject = new Item();
            }

            public class Item
            {
                [DefaultValue(5)]
                [ReadOnlyAttribute(false)]
                public int Id { get; set; }
                
                [DefaultValue("Test")]
                [ReadOnlyAttribute(true)]
                public string Name { get; set; }
                public Item()
                {
                    this.Id = 5;
                    this.Name = "Test";
                }
            }

  5. Dess
    Admin
    Dess avatar
    2576 posts

    Posted 04 Jan Link to this post

    Hello, Valery, 

    Thank you for writing back. 

    The provided sample code snippet and screenshots are greatly appreciated. Indeed, RadPropertyGrid doesn't show the read-only properties as gray. We will consider such an improvement in the future releases. However, it can be easily achieved by using the ItemFormatting event:
    private void radPropertyGrid1_ItemFormatting(object sender, PropertyGridItemFormattingEventArgs e)
    {
        PropertyGridItem item = e.Item as PropertyGridItem;
        if (item != null && item.ReadOnly)
        {
            e.VisualElement.ForeColor = Color.Gray;
        }
        else
        {
            e.VisualElement.ForeColor = Color.Black;
        }
    }



    I hope this information helps. If you have any additional questions, please let me know. 

     Regards,
    Dess
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  6. Valery
    Valery avatar
    32 posts
    Member since:
    May 2011

    Posted 04 Jan in reply to Dess Link to this post

    Thank you. This works, but not for the font.

    Item "Font" not readonly and is drawn in black. But subitems of Font is drawn in gray

  7. Dess
    Admin
    Dess avatar
    2576 posts

    Posted 04 Jan Link to this post

    Hello, Valery, 

    Thank you for writing back. 

    You can extend the previous example and include a condition for checking the read-only property of the parent item: 
    private void radPropertyGrid1_ItemFormatting(object sender, PropertyGridItemFormattingEventArgs e)
    {
        PropertyGridItem item = e.Item as PropertyGridItem;
        Console.WriteLine(e.Item.Name);
        if (item != null && item.ReadOnly)
        {
            if (e.Item.Parent == null || (e.Item.Parent != null && ((PropertyGridItem)e.Item.Parent).ReadOnly))
            {
                e.VisualElement.ForeColor = Color.Gray;
            }
        }
        else
        {
            e.VisualElement.ForeColor = Color.Black;
        }
    }

    I hope this information helps. If you have any additional questions, please let me know. 

     Regards,
    Dess
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  8. Valery
    Valery avatar
    32 posts
    Member since:
    May 2011

    Posted 04 Jan in reply to Dess Link to this post

    This works for the font, but does not work for other situations.

    Example:

    item may be readonly, but parent != null and parent not readonly 

  9. Valery
    Valery avatar
    32 posts
    Member since:
    May 2011

    Posted 05 Jan in reply to Valery Link to this post

    Possible Solution:

     

                if (e.Item is PropertyGridItem item && item.ReadOnly)
                {
                    if (e.Item.Parent != null && ((PropertyGridItem)e.Item.Parent).PropertyType.Name.Equals("Font"))
                    {
                        e.VisualElement.ForeColor = Color.Black;
                    }
                    else
                    {
                        e.VisualElement.ForeColor = Color.Gray;
                    }
                }
                else
                {
                    e.VisualElement.ForeColor = Color.Black;
                }


  10. Dess
    Admin
    Dess avatar
    2576 posts

    Posted 05 Jan Link to this post

    Hello, Valery, 

    Thank you for writing back. 

    The provided code snippet will cover the case with the Font. If you have other nested properties which are read-only but the parent is not read-only, checking the property name in order to determine what would be the forecolor is a suitable approach. Feel free to use it in order to achieve the desired formatting.

    I hope this information helps. If you have any additional questions, please let me know. 

     Regards,
    Dess
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top