Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to use NotifyParentPropertyAttribute

How to use NotifyParentPropertyAttribute

Scheduled Pinned Locked Moved C#
questionhelptutorialannouncement
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jjansen
    wrote on last edited by
    #1

    I'm working on an application that displays properties of an object in a PropertyGrid control. This object's properties don't have attributes (like BrowsableAttribute, CategoryAttribute, etc.). Since I want to control how and which properties are displayed in the PropertyGrid and in which categories and so on, I have made a "wrapper-class". It copies the required data from the original object and makes it accessible in custom properties which are not a one-to-one representation of the properties of the original object (instead they are split-up into more user friendly ones). The wrapper-object stores a reference to the original object as well. Some of these properties in my wrapper-class access objects which have properties themselves. With the use of the TypeConverterAttribute I am able to show and edit nested properties. When the user is done making changes in the PropertyGrid, I make the wrapper-object transfer its data to the original object. It works, but I want to leave out this last step. I want the wrapper-object to update the data in the original object at the same time as when a property in the wrapper-class is changed. This causes a problem when the properties are nested, like mine are. When the properties of an object that is accessed by a property in the wrapper-class is changed, how will the wrapper-object know? I found an attribute that may do the trick: NotifyParentPropertyAttribute. This is a flag saying you want the parent property to be notified by a changed child property. However, I haven't a clue how (or if) the property should send this notification and how the parent should receive it. So my question is: what should I do to be able to handle this notification. Has anyone come across this attribute before? Thanks.

    H 1 Reply Last reply
    0
    • J jjansen

      I'm working on an application that displays properties of an object in a PropertyGrid control. This object's properties don't have attributes (like BrowsableAttribute, CategoryAttribute, etc.). Since I want to control how and which properties are displayed in the PropertyGrid and in which categories and so on, I have made a "wrapper-class". It copies the required data from the original object and makes it accessible in custom properties which are not a one-to-one representation of the properties of the original object (instead they are split-up into more user friendly ones). The wrapper-object stores a reference to the original object as well. Some of these properties in my wrapper-class access objects which have properties themselves. With the use of the TypeConverterAttribute I am able to show and edit nested properties. When the user is done making changes in the PropertyGrid, I make the wrapper-object transfer its data to the original object. It works, but I want to leave out this last step. I want the wrapper-object to update the data in the original object at the same time as when a property in the wrapper-class is changed. This causes a problem when the properties are nested, like mine are. When the properties of an object that is accessed by a property in the wrapper-class is changed, how will the wrapper-object know? I found an attribute that may do the trick: NotifyParentPropertyAttribute. This is a flag saying you want the parent property to be notified by a changed child property. However, I haven't a clue how (or if) the property should send this notification and how the parent should receive it. So my question is: what should I do to be able to handle this notification. Has anyone come across this attribute before? Thanks.

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      There's a couple more robust ways of doing this: implement ICustomTypeConverter on the "design-time unfriendly" object, or implement a designer. Both allow you to return a PropertyDescriptorCollection that contains PropertyDescriptors. A designer can also allow you to pre- and post-filter properties on an object. Through custom PropertyDescriptor implementations, you can configure the categories, descriptions, read-only'ness, etc. for a property, as well as call it anything you want (and implement GetValue and SetValue however you want). I use this for a preference manager with a temporary PropertyGrid to edit the singleton data (through the internal static instance field that returns the singleton). I implement ICustomTypeDescriptor and - for the most part - just use the TypeDescriptor methods related to whatever method I'm implementing for the ICustomTypeDescriptor. For GetProperties, I use a custom PropertyDescriptor class that actually encapsulates rows in a database (via a DataSet that is filled/updated based on conditions) and displays them as properties on an object (with full support via the Type for TypeConverters, UITypeEditors, etc.). This way, the user is actually changing the object without you having to shadow properties like you are now. The NotifyParentPropertyAttribute simple causes the designer to use the information it has (such as what's returned for TypeConverter.GetCreateInstanceSupported) to re-create an object based on the new values in the property attributed with the NotifyParentPropertyAttribute. Very little else is documented about it, and since the implementation is primarily in the designer (i.e., the VS.NET IDE or #develop IDE, etc.) using the IL disassembler (or any disassembler/decompiler) won't be very helpful. I recommend using ICustomTypeDescriptor or a designer.

      Microsoft MVP, Visual C# My Articles

      J 1 Reply Last reply
      0
      • H Heath Stewart

        There's a couple more robust ways of doing this: implement ICustomTypeConverter on the "design-time unfriendly" object, or implement a designer. Both allow you to return a PropertyDescriptorCollection that contains PropertyDescriptors. A designer can also allow you to pre- and post-filter properties on an object. Through custom PropertyDescriptor implementations, you can configure the categories, descriptions, read-only'ness, etc. for a property, as well as call it anything you want (and implement GetValue and SetValue however you want). I use this for a preference manager with a temporary PropertyGrid to edit the singleton data (through the internal static instance field that returns the singleton). I implement ICustomTypeDescriptor and - for the most part - just use the TypeDescriptor methods related to whatever method I'm implementing for the ICustomTypeDescriptor. For GetProperties, I use a custom PropertyDescriptor class that actually encapsulates rows in a database (via a DataSet that is filled/updated based on conditions) and displays them as properties on an object (with full support via the Type for TypeConverters, UITypeEditors, etc.). This way, the user is actually changing the object without you having to shadow properties like you are now. The NotifyParentPropertyAttribute simple causes the designer to use the information it has (such as what's returned for TypeConverter.GetCreateInstanceSupported) to re-create an object based on the new values in the property attributed with the NotifyParentPropertyAttribute. Very little else is documented about it, and since the implementation is primarily in the designer (i.e., the VS.NET IDE or #develop IDE, etc.) using the IL disassembler (or any disassembler/decompiler) won't be very helpful. I recommend using ICustomTypeDescriptor or a designer.

        Microsoft MVP, Visual C# My Articles

        J Offline
        J Offline
        jjansen
        wrote on last edited by
        #3

        Thanks. I will look into it right away!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups