Atributes

5 posts, 1 answers
  1. david
    david avatar
    25 posts
    Member since:
    Oct 2008

    Posted 24 Aug 2011 Link to this post

    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.
  2. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 24 Aug 2011 Link to this post

    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 >>

  3. UI for WPF is Visual Studio 2017 Ready
  4. david
    david avatar
    25 posts
    Member since:
    Oct 2008

    Posted 24 Aug 2011 Link to this post

    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.
  5. Answer
    Shaun
    Shaun avatar
    1 posts
    Member since:
    Aug 2011

    Posted 25 Aug 2011 Link to this post

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

  6. david
    david avatar
    25 posts
    Member since:
    Oct 2008

    Posted 25 Aug 2011 Link to this post

    Thanks!!
Back to Top
UI for WPF is Visual Studio 2017 Ready