This is a migrated thread and some comments may be shown as answers.

Different subclass show different property

1 Answer 80 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 1
Veteran
Steve asked on 14 Jan 2020, 08:42 AM

I have a hierarchy

public class Corner
    {
    public string Name { get; set; }
    }
 
public class Fillet : Corner
    {
    public double Radius { get; set; }
    }
 
public class Chamfer : Corner
    {
    public double Distance1 { get; set; }
    public double Distance2 { get; set; }
    }

and a business object,

public enum CornerMode
{
Fillet,
Chamfer
}
 
public class CornerAttribute
{
public CornerMode CornerMode {get;set;}
public Corner Corner {get;set;}
}

 

I will assign a subclass instance according to the CornerMode. How to switch the type convert at runtime so that the property Corner can be expandable?

1 Answer, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 16 Jan 2020, 12:13 PM

Hello Steve,

In order to make "Corner" property expandable, you can bound TypeConverter attribute of type ExpandableObjectConverter to "Corner" as follows:

public class Profession
{
    public string Title { get; set; }

    [Description("This is a property with a ExpandableObjectConverter")]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public Corner Corner { get; set; }
}

If you wish to hide "Corner" text, as shown in your attachments, you can subscribe to RadPropertyGrid1_ItemFormatting event and hide the text.

private void RadPropertyGrid1_ItemFormatting(object sender, PropertyGridItemFormattingEventArgs e)
{
    if (e.Item.Label == "Corner")
    {
        ((PropertyGridItemElement)e.VisualElement).ValueElement.Text = "";
    }
}

You can also disable editing of "Corner" text by subscribing to RadPropertyGrid1_Editing event and cancel editing as follows:

private void RadPropertyGrid1_Editing(object sender, PropertyGridItemEditingEventArgs e)
{
    if (e.Item.Label == "Corner")
    {
        e.Cancel = true;
    }
}

For your convenience, I have attached a project with an implementation and a screenshot.

I hope you find this information useful. Please don't hesitate to ask if you have further questions.

Regards,
Dimitar
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
PropertyGrid
Asked by
Steve
Top achievements
Rank 1
Veteran
Answers by
Dimitar
Telerik team
Share this question
or