Display a different custom set of PropertyDefinitions based on object type?

7 posts, 1 answers
  1. Robert
    Robert avatar
    15 posts
    Member since:
    Mar 2013

    Posted 22 Aug 2013 Link to this post

    I'm trying to implement a property editor control for an application using the RadPropertyGrid. The user will select from a list of objects and the propertygrid editor needs to change based on the object type. That would be perfectly fine if we were giving access to ALL properties of each object, but we only want to display specific subset of each object to the end user, and that subset will vary based on each object type. For example, an object of type Circle we may want to display the center point coordinates and a radius, while a Rectangle we want to display lower-left coordinates and a width and height. These object classes are defined in a 3rd party library, and each of them have many other properties we don't want to expose.

    Ideally it would be great if we could set AutoGeneratePropertyDefinitions to false and define a different set of RadPropertyGrid.PropertyDefinitions for each type, then switch that set of definitions based on the object type. I've used converters to change controls' style properties based on object types before, but since since the PropertyDefinitions isn't a property itself, I can't use that same technique.

    Any suggestions?

    Thanks!
  2. Answer
    Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1128 posts

    Posted 27 Aug 2013 Link to this post

    Hello,

    Basically, the best approach would be to use the Browsable attribute. Setting it to false, would make RadPropertyGrid skip the respective property. However, I am not sure whether you can easily do this, as you have mentioned some third party assembly. The other, in my opinion acceptable, approach is to maintain some data structure (i.e. a Dictionary) with the list of properties that should be displayed for a certain type. Then, in the AutoGeneratingPropertyDefinition handler the PropertyDefinitions, which DisplayName does not match any one listed in the data structure, gets canceled, by setting args.Cancel = true.

    Regards,
    Ivan Ivanov
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Robert
    Robert avatar
    15 posts
    Member since:
    Mar 2013

    Posted 30 Aug 2013 Link to this post

    Thanks for your help! I ended up doing a variation of what you suggested. Basically I created a new class is a list of property names but also has a property that defines an object type that it should be tied to. Then I extended the RadPropertyGrid class to have a new property that accepted a list of instances of this new class. Since the RadPropertyGrid doesn't have an event that fires when its Item property changes (or, if its an INotifyCollectionChange item, an event when the collection changes), I overrode the Item property with my dependency property which fires a change event.  That change event can then handle choosing the appropriate property list(s) for the object(s) being bound to the PropertyGrid.

    Finally I subscribed to the AutoGeneratingPropertyDefinition and in the handler checked to see if the property being generated was in the property list, and if it wasn't cancelled the events.

    With this technique I'm able to create xaml definitions for each object type I have and the list of properties that should be displayed for each one, and bind them to the extended RadPropertyGrid control and filter the object's properties that way.

    Thanks again for your help!
  5. De
    De avatar
    16 posts
    Member since:
    Aug 2011

    Posted 02 May 2014 Link to this post

    Another option is to make the DataTemplateSelector more intelligent (through EditorTemplateSelector).  Since the DataTemplateSelector knows when 'item' changes as well as it can own a list of DataTemplates (you have to inject in), the DataTemplateSelector can check the type of item (as DataContext) and selects the appropriate DataTemplate.  
  6. Mahdi
    Mahdi avatar
    8 posts
    Member since:
    Jan 2014

    Posted 12 May 2014 Link to this post

    Hello,

    I have RadPropertyGrid and want to bind Item Property to variable of type "object". When I run program, objects with different properties created and their properties must be shown in PropertyGrid. But always property of first created object displayed and didn't changed for other objects.
    Is there any sample that show Robert solution for overriding RadPropertyGrid or any property that I could set in RadPropertyGrid and solve this problem.

    Sincerely,
  7. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1128 posts

    Posted 15 May 2014 Link to this post

    Hello,

    Well, it seems that your scenario is different from the original one, as you are not aiming to display only a limited set of properties, am I right? Basically, RadPropertyGrid automatically rebinds its data engine as soon as the Item property gets its value changed. Do you raise the PropertyChanged event after the bound property gets updated? In case this is not cause, can you please send us a sample dummy project that we can debug on our side?

    Regards,
    Ivan Ivanov
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  8. Mahdi
    Mahdi avatar
    8 posts
    Member since:
    Jan 2014

    Posted 28 May 2014 in reply to Ivan Ivanov Link to this post

    your right, Ivan Ivanov.

    I use PostSharp for adding PropertyChanged to my class properties. But after many inspecting in my code, I realized that because of some mistake in my Multicasting, this event not added to my code and cause error like that.

    Thank you for your advice.
Back to Top
UI for WPF is Visual Studio 2017 Ready