Wpf user control: makes complex properties editabl

2019-06-07 05:40发布

问题:

Suppose you create a standard UserControl or a CustomControl in wpf and then declare a property of a complex type

public ComplexPropertyType AProperty { get; set; }

Complex type is a simple class with a bunch of properties

public class ComplexPropertyType 
{
    public int IntP { get; set; }

    public String Strp { get; set; }
}

At design time AProperty is not editable.

I remember that there are attributes from System.ComponentModel that permits me to specify to use a standard property editor to make that property editable, but I cannot remember.

Anyone has a solution that clearly does not require writing a custom property editor? After all If I declare in my control a property that is a collection of complex type Es.

public ObservableCollection ACollOfProperty { get; set; }

Automatically the designer use the System.ComponentModel.Design.CollectionEditor that is capable of editing my complex type

Thanks in advance.

回答1:

In wpf you can use this http://wpg.codeplex.com/ or use this winform contro



回答2:

I was able to find the attribute myself, is the TypeConverter. You should declare the property in user control with TypeConverterAttribute specifying the ExpandableObjectconverter

    [TypeConverter(typeof(ExpandableObjectConverter))]
    public ComplexPropertyType AProperty { get; set; }

Now you should see in the designer a nice NEW button that permits you to populate the value at design time

Pressing new it will create an instance of the ComplexPropertyType, so it is editable directly from the designer.

Alk.