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

Atributes

4 Answers 265 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
david
Top achievements
Rank 1
david asked on 24 Aug 2011, 07:52 AM
Hi,

Is it possible that the propertygrid content is filled with a class that has attributes, like
the winform propertygrid?

for example, I have a class from a winform application with some attributes that are shown
in a winform propertygrid
 some attributes are:

   [Category("Description")]
     
 or 
  [CategoryAttribute("Properties"), DefaultValueAttribute(false)]
or
[Category("Description"), Browsable(false)]   <- this one will not show up in the propertygrid

I noticed that I see all the properties for the class even the properties I don't want the user to see or fill

David.

4 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 24 Aug 2011, 09:23 AM
Hi David,

The attributes stated are not internally supported.  However there is an easy way to take control with a few additional lines of code .
Basically what you need to do is to handle the AutoGeneratingPropertyDefinition event of RadPropertyGrid.
Inside the handler you can either cancel the creation of the property definition ( e.Cancel - true) , or set its category by setting the GroupName of the property definition.

In case this sounds complicated , you can just paste me the code of your business object ( the one you need edited in RadPropertyGrid) and I will gather a small sample app for you with your object.


Regards,
Pavel Pavlov
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Al
Top achievements
Rank 1
commented on 04 May 2021, 02:52 PM

Attributes are now supported, but I don't understand how I can sort not only properties within a group but also the groups themselves. With WinForm I can put "\ t" in CategoryAttribute before my group name to force my custom sorting between groups.
Dinko | Tech Support Engineer
Telerik team
commented on 07 May 2021, 07:59 AM

Hello Albino Vallino,

RadPropertyGrid sort only the list of property definitions. When there is grouping, the group, which has the property definition that is first in the sorted list, will be displayed first. To override this behavior, you can add alternative sorting criteria by using the OrderIndex property of PropertyDefinition, or DisplayAttribute.Order. If the property definitions are autogenerated, you can use the AutoGeneratePropertyDefinitions event and then set the OrderIndex property of the PropertyDefinition instance passed in the args.

0
david
Top achievements
Rank 1
answered on 24 Aug 2011, 04:21 PM
Hi Pavel,

thanks for the fast response, I've tried to handle the AutoGeneratingPropertyDefinition event of the propertygrid but I could not
make it work.

The code below a sample class with some attributes from my class, I would be very happy if you could make a example for me.

 class TestPropertyGrid
    {
        [Category("Maintenance"), DefaultValue("")]
        public string Description { get; set; }

        [Category("Maintenance")]
        public int Id { get; set; }

        [Category("Maintenance"), DefaultValue(true)]
        public bool IsRequired { get; set; }

        [Category("Properties"), DefaultValue(true), Browsable(false)]
        public bool IsChangeRequired { get; set; }

        [Editor(typeof(IconFiles), typeof(System.Drawing.Design.IconEditor)), CategoryAttribute("Icon"), DefaultValueAttribute(true)]
        public string Iconfile { get; set; }        
    }

    public class IconFiles : IconEditor
    {
        string IconPath = @"C:\Temp";
        public IconFiles() { }

        protected override void InitializeDialog(OpenFileDialog openFileDialog)
        {
            openFileDialog.Title = "Icons";
            openFileDialog.Multiselect = false;
            openFileDialog.InitialDirectory = IconPath;
            openFileDialog.Filter = "icons (*.ico)|*.ico|All files (*.*)|*.*";
            openFileDialog.RestoreDirectory = true;
        }
    }

David.
0
Accepted
Shaun
Top achievements
Rank 1
answered on 25 Aug 2011, 07:26 AM
You can use reflection to retrieve the attributes, this is a snippet from my own implementation of AutoGeneratingPropertyDefinition (Just replace "InsertClassTypeHere" with your own System.Type representing the Class you are trying to display in the PropertyGrid. Hope this helps.

private void AutoGeneratingPropertyDefinition(object a_sender, Telerik.Windows.Controls.Data.PropertyGrid.AutoGeneratingPropertyDefinitionEventArgs a_e)
        {
              AttributeCollection attributes = TypeDescriptor.GetProperties(InsertClassTypeHere)[a_e.PropertyDefinition.DisplayName].Attributes;
 
              BrowsableAttribute browsable = attributes[typeof(BrowsableAttribute)] as BrowsableAttribute;
              if (browsable != null)
              {
                  // If browsable set to false cancel, otherwise display
                  a_e.Cancel = !browsable.Browsable;
              }
 
              DescriptionAttribute description = attributes[typeof(DescriptionAttribute)] as DescriptionAttribute;
              if (description != null)
              {
                  a_e.PropertyDefinition.Description = description.Description;
              }
        }

0
david
Top achievements
Rank 1
answered on 25 Aug 2011, 07:29 AM
Thanks!!
Tags
PropertyGrid
Asked by
david
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
david
Top achievements
Rank 1
Shaun
Top achievements
Rank 1
Share this question
or