Export (0) Print
Expand All

ExpandableObjectConverter Class

Provides a type converter to convert expandable objects to and from various other representations.

Namespace:  System.ComponentModel
Assembly:  System (in System.dll)

[HostProtectionAttribute(SecurityAction.LinkDemand, SharedState = true)]
public class ExpandableObjectConverter : TypeConverter

NoteNote:

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: SharedState. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

This class adds support for properties on an object to the methods and properties provided by TypeConverter. To make a type of property expandable in the PropertyGrid, specify this TypeConverter for standard implementations of GetPropertiesSupported and GetProperties. Mark child properties with the NotifyParentPropertyAttribute to ensure correct behavior in a PropertyGrid control.

NoteNote:

You should never access a type converter directly. Instead, call the appropriate converter by using TypeDescriptor. For more information, see the examples in the TypeConverter base class.

For more information about type converters, see the TypeConverter base class and How to: Implement a Type Converter.

The following code example converts a variable of type Margins to a string variable.

string strM="1,2,3,4";
System.Drawing.Printing.Margins  m= new System.Drawing.Printing.Margins(1,2,3,4);
Console.WriteLine(TypeDescriptor.GetConverter(strM).CanConvertTo(typeof(System.Drawing.Printing.Margins))); 
Console.WriteLine(TypeDescriptor.GetConverter(m).ConvertToString(m)); 

The following code example demonstrates how to use the NotifyParentPropertyAttribute and the ExpandableObjectConverter class to create an expandable property on a custom control. This code example is part of a larger example provided for the NotifyParentPropertyAttribute class.

[TypeConverter(typeof(BorderAppearanceConverter))]
public class BorderAppearance
{
    private int borderSizeValue = 1;
    private Color borderColorValue = Color.Empty;

    [Browsable(true),
    NotifyParentProperty(true),
    EditorBrowsable(EditorBrowsableState.Always),
    DefaultValue(1)]
    public int BorderSize
    {
        get
        {
            return borderSizeValue;
        }
        set
        {
            if (value < 0)
            {
                throw new ArgumentOutOfRangeException(
                    "BorderSize",
                    value,
                    "must be >= 0");
            }

            if (borderSizeValue != value)
            {
                borderSizeValue = value;
            }
        }
    }

    [Browsable(true)]
    [NotifyParentProperty(true)]
    [EditorBrowsable(EditorBrowsableState.Always)]
    [DefaultValue(typeof(Color), "")]
    public Color BorderColor
    {
        get
        {
            return borderColorValue;
        }
        set
        {
            if (value.Equals(Color.Transparent))
            {
                throw new NotSupportedException("Transparent colors are not supported.");
            }

            if (borderColorValue != value)
            {
                borderColorValue = value;
            }
        }
    }
}

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft