locked
Nesting properties in property grid RRS feed

  • Question

  • Hi all,

    Is it possible to have multiple nesting, like nesting within nesting in the property grid? for example, I want to create an xml file in my program and use it to populate my grid. In the propertyGrid, I want to be able to nest within a nest, so product suite will be expandable/collapsable to show product names one and two and product names one and two will also need to be expandable/collapsable to display a sub product name and date.

    Is this possible with property grid? please let me know how i can achieve this.  Thanks in advance

    <ProductSuite>

    <Product NameOne=”Product 1”>

    <SubProduct Name=”Sub Product 1” Date = "2004, 12, 09"/>

    </Product>

    <Product NameTwo=”Product 2”>

    <SubProduct Name=”Sub Product 2” Date = "2004, 12, 09"/>

    </Product>

     </ProductSuite>

     

     



    • Edited by _Priceless Thursday, November 3, 2011 10:29 AM
    • Moved by Leo Liu - MSFT Friday, November 4, 2011 3:35 AM Moved for better support. (From:Visual C# General)
    Thursday, November 3, 2011 10:19 AM

Answers

  • You don't need any 'Blank' property. You can return an empty string from ToString:

    public override string ToString() 
    {
            return string.Empty;
    }
    

     

    • Marked as answer by _Priceless Thursday, November 10, 2011 11:57 AM
    Thursday, November 10, 2011 11:18 AM
  • Thats what I have been doing, but I can't do the nesting using this technique. I would like to nest a category within another category. the main category called productSuite will have the (+) and within that category it will have two properties Product one and Product two with (+) signs and within them a property called sub property which also has its own properties.

    Can someone tell me how i can achieve this using the property grid 


    To have the (+) sign appear on a property, you can put a TypeConverter attribute on the property:

    [TypeConverter(typeof(ExpandableObjectConverter))]
    public object propertyToExpand { get; set; }
    

     

    • Proposed as answer by Mike Dos Zhang Monday, November 7, 2011 8:33 AM
    • Marked as answer by Mike Dos Zhang Wednesday, November 9, 2011 9:23 AM
    Monday, November 7, 2011 8:04 AM
  • that doesnt happen on mine and I have used the exact same code as you.  except i dnt have "some string" as its attribute name


    The ExpandableObjectConverter is used to display the properties of the object returned by the Name property. If you didn't set the Name property to anything, it returns null. And null doesn't have any property.

    If that behavior is not what you expect, you can use one of the other existing TypeConverters (there is a list here: http://msdn.microsoft.com/en-us/library/system.componentmodel.typeconverter.aspx), or create your own, possibly deriving from ExpandableObjectConverter.

    • Marked as answer by Mike Dos Zhang Wednesday, November 9, 2011 9:22 AM
    Monday, November 7, 2011 12:19 PM
  • I have ran your program and name does not expand. I have changed frameworks etc. Has it got to do with the version of IDE i am using? I'm using VC sharp express edition.

    As already explained, you cannot expand the 'Name' property because you haven't set 'Name' to anything. A null value doesn't have properties.
    • Marked as answer by Mike Dos Zhang Wednesday, November 9, 2011 9:22 AM
    Wednesday, November 9, 2011 7:49 AM
  • Yes,

    If your code is like this, the second property is a type with properties, then you can expand it.

        [TypeConverter(typeof(ExpandableObjectConverter))]
        //[DataContract]
        public class Property
        {
            public Property()
            {
            }
            //[DataMember]
            DateTime m_DisplayInt;
            [Browsable(true)]
            [Description("sample hint1")]
            [Category("book")]
            [DisplayName("Time")]
            public DateTime DisplayInt
            {
                get { return m_DisplayInt; }
                set { m_DisplayInt = value; }
            }
            //[DataMember]
            [Browsable(true)]
            [Description("book")]
            [Category("Category One")]
            [DisplayName("Name")]
            [TypeConverter(typeof(ExpandableObjectConverter))]
            public DisplayStringProperty DisplayString
            {
                get { return displayString; }
                set { displayString = value; }
            }
            private DisplayStringProperty displayString = new DisplayStringProperty() { abc = "asdfas", cd = 12 };
            //public string DisplayString
            //{
            //    get { return displayString; }
            //    set { displayString = value; }
            //}
            //private string displayString = "adfas";
        }
    
        public class DisplayStringProperty 
        {
            public string abc { get; set; }
            public int cd { get; set; }
        }
    



    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by _Priceless Wednesday, November 9, 2011 9:01 AM
    Wednesday, November 9, 2011 8:14 AM

All replies

  • Create a class which implements ICustomTypeDescriptor, with GetProperties returning properties based on the xml.
    Thursday, November 3, 2011 10:36 AM
  • Will ICustomTypeDescriptor allow me to specify what types I want my attributes to be? for instance Date will be a DateTime property.
    Thursday, November 3, 2011 10:42 AM
  • Hi, I have used TypeConverter and inherited from ExpandableObjectConverter, but I couldn't specify my types for each property in my code because it was just reading from the xml file converting them in my convert class etc and displaying them on the grid only as a string. For example,I have a property called Startdate I would like that property to only accpet dates, I would also like certain properties to be integers, however using TypeConverter I wasnt able to do this, is it even possible to specify each property type using TypeConverters??

    Because this was the case i reverted to actually specifying the property types in the class, however i am unable to do multiple nesting. Quite annoying.

    Thursday, November 3, 2011 12:21 PM
  • If your class' constructor takes xml as parameter, nesting should be possible by creating another instance of the same class based on the nested xml. I don't have time right now to test it, but curious as I am, I may try to do something like that later today or tomorrow.
    Thursday, November 3, 2011 1:15 PM
  • I want to be able to specify the property type too, e.g DateTime, Integer, etc

    Thursday, November 3, 2011 1:29 PM
  • It looks like you just want to let the PropertyGrid display the content as layers in the xml document, then I think you can consider these options:

    http://stackoverflow.com/questions/4591115/how-to-load-xml-document-in-property-grid

    http://www.codeproject.com/KB/grid/XmlGridControl.aspx

     


    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, November 4, 2011 5:54 AM
  • Thats not what I want, I have done it that way already and I cannot get to specify the type on the property grid, like I said before the property has no input validation, it accepts all strings. I want to have a Date type, int type etc has some of my properties. 

     

    Anyways, is it at all possible to have categories within categories like in my xml file on the property grid? and if so how?

    Friday, November 4, 2011 6:41 PM
  • If the requirement is like this, then I think this process is like a deserialization. We should be use a type/class to receive the data and create the typed new instance, then let PropertyGrid select this instance, then it will do the job as usual as before.
    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Saturday, November 5, 2011 4:44 AM
  • Thats what I have been doing, but I can't do the nesting using this technique. I would like to nest a category within another category. the main category called productSuite will have the (+) and within that category it will have two properties Product one and Product two with (+) signs and within them a property called sub property which also has its own properties.

    Can someone tell me how i can achieve this using the property grid 

    Saturday, November 5, 2011 12:32 PM
  • Thats what I have been doing, but I can't do the nesting using this technique. I would like to nest a category within another category. the main category called productSuite will have the (+) and within that category it will have two properties Product one and Product two with (+) signs and within them a property called sub property which also has its own properties.

    Can someone tell me how i can achieve this using the property grid 


    To have the (+) sign appear on a property, you can put a TypeConverter attribute on the property:

    [TypeConverter(typeof(ExpandableObjectConverter))]
    public object propertyToExpand { get; set; }
    

     

    • Proposed as answer by Mike Dos Zhang Monday, November 7, 2011 8:33 AM
    • Marked as answer by Mike Dos Zhang Wednesday, November 9, 2011 9:23 AM
    Monday, November 7, 2011 8:04 AM
  • Louis.fr said is right, we need the ExpandableObjectConverter to mark your property when you define this property.
    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, November 7, 2011 8:34 AM
  • the plus sign does nto appear on the name property and i have put the attribute [TypeConverter(typeof(ExpandableObjectConverter))]

    What am i doing wrong please

     

     

    [

    DataContract

    ]

     

    public class

    Property

    {

     

     

    public

    Property()

    {

    }

     

    [

    DataMember

    ]

     

    DateTime

    m_DisplayInt;

    [

    Browsable(true

    )]

    [

    Description("sample hint1"

    )]

    [

    Category("book"

    )]

    [

    DisplayName("Time"

    )]

     

    public DateTime

    DisplayInt

    {

     

    get { return

    m_DisplayInt; }

     

    set { m_DisplayInt = value

    ; }

    }

    [

    DataMember

    ]

    [

    Browsable(true)]

     

    [

    Description("book")]

     

    [

    Category("Category One")]

     

    [

    DisplayName("Name")]

    [TypeConverter(typeof(ExpandableObjectConverter

    ))]

     

    public string

    DisplayString

    {

    get; set

    ; }

     

     

     

     

    }

    Monday, November 7, 2011 9:24 AM
  • the plus sign does nto appear on the name property

    It should. I copied your exact code, created a Property object, and set the SelectedObject property of the PropertyGrid and the (+) appears next to the Name property. When expanded, it shows the only property of the string: Length.
    Monday, November 7, 2011 10:14 AM
  • all thats expandable is "category one" the name property itsn't.
    Monday, November 7, 2011 10:26 AM
  • Monday, November 7, 2011 10:43 AM
  • that doesnt happen on mine and I have used the exact same code as you.  except i dnt have "some string" as its attribute name

    From your example is the display name "name" within "category one"?  
    • Edited by _Priceless Monday, November 7, 2011 12:18 PM
    Monday, November 7, 2011 12:03 PM
  • that doesnt happen on mine and I have used the exact same code as you.  except i dnt have "some string" as its attribute name


    The ExpandableObjectConverter is used to display the properties of the object returned by the Name property. If you didn't set the Name property to anything, it returns null. And null doesn't have any property.

    If that behavior is not what you expect, you can use one of the other existing TypeConverters (there is a list here: http://msdn.microsoft.com/en-us/library/system.componentmodel.typeconverter.aspx), or create your own, possibly deriving from ExpandableObjectConverter.

    • Marked as answer by Mike Dos Zhang Wednesday, November 9, 2011 9:22 AM
    Monday, November 7, 2011 12:19 PM
  • it is what i want, i want categories within categories. It's just not working for me at the moment

    Monday, November 7, 2011 2:32 PM
  • https://skydrive.live.com/embedicon.aspx/2011/2011Y11M/PropertyGridCategory.zip?cid=bb789f72272d4858&sc=photos

    I also tried the code in your post, it works as fine as Louis said.

    Can you help us to test this project in your side?


    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, November 8, 2011 11:54 AM
  • I have ran your program and name does not expand. I have changed frameworks etc. Has it got to do with the version of IDE i am using? I'm using VC sharp express edition. This is what I see:


    • Edited by _Priceless Tuesday, November 8, 2011 6:26 PM
    Tuesday, November 8, 2011 6:25 PM
  • I have ran your program and name does not expand. I have changed frameworks etc. Has it got to do with the version of IDE i am using? I'm using VC sharp express edition.

    As already explained, you cannot expand the 'Name' property because you haven't set 'Name' to anything. A null value doesn't have properties.
    • Marked as answer by Mike Dos Zhang Wednesday, November 9, 2011 9:22 AM
    Wednesday, November 9, 2011 7:49 AM
  • Yes,

    If your code is like this, the second property is a type with properties, then you can expand it.

        [TypeConverter(typeof(ExpandableObjectConverter))]
        //[DataContract]
        public class Property
        {
            public Property()
            {
            }
            //[DataMember]
            DateTime m_DisplayInt;
            [Browsable(true)]
            [Description("sample hint1")]
            [Category("book")]
            [DisplayName("Time")]
            public DateTime DisplayInt
            {
                get { return m_DisplayInt; }
                set { m_DisplayInt = value; }
            }
            //[DataMember]
            [Browsable(true)]
            [Description("book")]
            [Category("Category One")]
            [DisplayName("Name")]
            [TypeConverter(typeof(ExpandableObjectConverter))]
            public DisplayStringProperty DisplayString
            {
                get { return displayString; }
                set { displayString = value; }
            }
            private DisplayStringProperty displayString = new DisplayStringProperty() { abc = "asdfas", cd = 12 };
            //public string DisplayString
            //{
            //    get { return displayString; }
            //    set { displayString = value; }
            //}
            //private string displayString = "adfas";
        }
    
        public class DisplayStringProperty 
        {
            public string abc { get; set; }
            public int cd { get; set; }
        }
    



    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by _Priceless Wednesday, November 9, 2011 9:01 AM
    Wednesday, November 9, 2011 8:14 AM
  • o wow thanks.

    Wednesday, November 9, 2011 9:01 AM
  • You're welcome!

     


    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, November 9, 2011 9:22 AM
  • Just one more question, why does the namespace and class get displayed in the nested categories value/property 'XML.serialization.DisplayStringProperty'. How can I get rid of it?

    Wednesday, November 9, 2011 10:08 AM
  • Can you help us describe your question clearer, it is a little hard to understand your this question.

    If there's any concern, please feel free to let me know.

    Best wishes,


    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, November 9, 2011 12:04 PM
  • Just one more question, why does the namespace and class get displayed in the nested categories value/property 'XML.serialization.DisplayStringProperty'. How can I get rid of it?

    That's the default return value of the ToString method: the name of the class. You can override ToString in the DisplayStringProperty class.

    Wednesday, November 9, 2011 12:13 PM
  • Can you give me an example of how to override the ToString method so that the namespace and class name is not displayed please.


    It would help if you could give me an example of how to do this...
    • Edited by _Priceless Wednesday, November 9, 2011 8:18 PM
    Wednesday, November 9, 2011 2:17 PM
  • We only need to insert those codes in the DisplayStringProperty class body in my above third post.

            public override string ToString()
            {
                return abc+"||"+cd.ToString();
            }
    
    Then you can test it.


    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, November 10, 2011 6:44 AM
  • Hi, tested it and it basically displays both values on the nested category's property. I want this to be blank, I don't want anything to be printed there. Is that possible to do when overriding the ToString method?

    To avoid anything been displayed there for now, I have just created a new string property and intialised it as a blank string value. But I don't know if that is good coding practice. And of course its also being displayed in the property grid as a property. i did this.

     

    public string Blank { get; set

    ; }

     

     

    public override string

    ToString()

    {

     

    return

    Blank.ToString();

    }

    Blank =

    " "



    • Edited by _Priceless Thursday, November 10, 2011 10:17 AM
    Thursday, November 10, 2011 10:08 AM
  • You don't need any 'Blank' property. You can return an empty string from ToString:

    public override string ToString() 
    {
            return string.Empty;
    }
    

     

    • Marked as answer by _Priceless Thursday, November 10, 2011 11:57 AM
    Thursday, November 10, 2011 11:18 AM
  • Thanks Louis
    Thursday, November 10, 2011 11:59 AM
  • Yes, that is.

     


    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, November 11, 2011 5:38 AM