Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
585
Use plus button to create an instance of a derived class
posted

Hi,

Given the following class structure:


    public class Person
    {
        public string Name { get; set; }
        public Animal Pet { get; set; }
    }

    public abstract class Animal
    {
        public string Name { get; set; }
    }

    public class Dog : Animal
    {
        public int TailWagginessFactor { get; set; }
    }

    public class Fish : Animal
    {
        public int MemoryDurationSeconds { get; set; }
    }

If I point the property grid at a Person object with a null Pet value, then I don't see the plus button to create an instance of Animal. I'd like some way to tell the grid (or it automatically detect) that Animal could be a Dog or a Fish and give me a plus button that offers me an option. As it is, I don't get the plus button. If I make Animal a concrete base class then I get the plus, but it just (unsurprisingly) creates an Animal object.

I appreciate I could probably create an PropertyGridEditorDefinition to give me some kind of selector to choose the type if it was null, but then I have to implement the property editors for the class myself, which defeats the point of using the property grid.

I could probably also override the OptionsMenu for the property grid, but that is global and my property grid is a lot more complex than this example, so not ideal. This would also probably be slightly confusing for the user, as the plus button and menu options appear in different places.

Any thoughts on any other ways to do this?

Thanks,

Richard

Parents
  • 34430
    Verified Answer
    Offline posted

    Hello Richard,

    Thank you for your update.

    The reason that you cannot create an instance of Animal at first is because when Animal is abstract, it cannot be instantiated. In other words, if you were to write Animal animal = new Animal(), you would receive an error because Animal is abstract and cannot be instantiated that way. For this reason, we hide the add-new button if the underlying type cannot be created directly.

    Between overriding the OptionsMenu and the usage of a PropertyGridEditorDefinition, I would recommend the PropertyGridEditorDefinition in this case, as this would allow you to define your own "template" that would allow you to pick your type and assign it to the underlying property.

    As another workaround that wasn't mentioned, you could also pull in the default style for the PropertyGridExpandableObjectEditorControl, which contains the "PART_CreateExpandableObjectButton" in its template that represents this "add new" button. This default style can be found in the generic.shared.xaml file commonly found at the following directory:

    C:\Program Files (x86)\Infragistics\<your version here>\WPF\DefaultStyles\PropertyGrid

    Doing this would allow you to overwrite the existing command with your own, and at that point you could perhaps pop up a dialog window that would allow you to choose which type you wish to place in that field. I have attached a sample project to demonstrate how this could be done.

    I think it's certainly possible that in a future version of the XamPropertyGrid that we could add this functionality, and if you would like to reinforce the chance of this getting added, I would recommend suggesting a new product idea for it at http://ideas.infragistics.com. This product ideas site will place you in direct communication with our product management teams who plan and prioritize upcoming features and development based on community and user feedback.

    Please let me know if you have any other questions or concerns on this matter.

    Sincerely,
    Andrew
    Associate Developer

    XamPropertyGridAddNewCase.zip
Reply Children
No Data