MRS1367
MRS1367

Reputation: 1053

How can edit field of an Expandable Property in the property grid?

I created a custom control with several properties. I add some expandable property to my custom control. Now, I want that user can edit expandable property field in the property grid and new entered values set for their related properties. My expandable property in the property grid is "Required Sign" and has two sub properties as follows:

  1. ForeColor

  2. Visible

I set two sub properties' values of the "Required Sign" expandable property to the field of the "Required Sign" property as you can see in the following figure:

"Required Sign" expandable property

  1. Green Box: "Required Sign" Expandable Property

  2. Blue Box: Two sub properties of the "Required Sign" Expandable Property

  3. Red Box: Field of the "Required Sign" Expandable Property

However, I can't change or edit field values of the "Required Sign" expandable property directly. How can I change or edit field values of expandable property (Red Box in the figure)?

My codes as follows:

[DisplayName("Label Information")]
[Description("Label Informationnnnnnnnnnnnnnnn")]
[DefaultProperty("Text")]
[DesignerCategory("Component")]
[TypeConverter(typeof(AllFloorsContentsLabelInformationTypeConverter))]
public class AllFloorsContentsLabelInformation : LabelX
{
    private AllFloorsContentsLabelRequiredSignInformation allFloorsContentsLabelRequiredSignInformation = new AllFloorsContentsLabelRequiredSignInformation();

    public AllFloorsContentsLabelInformation()
    {

    }

    [Category("Data")]
    [DisplayName("Required Sign")]
    [Description("Required Signnnnnnnnnnnnnnn")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public AllFloorsContentsLabelRequiredSignInformation AllFloorsContentsLabelRequiredSignInfo
    {
        get
        {
            return allFloorsContentsLabelRequiredSignInformation;
        }
    }
}

[DisplayName("Required Sign Information")]
[Description("Required Sign Informationnnnnnnnnnnnnnnn")]
[DefaultProperty("Text")]
[DesignerCategory("Component")]
[TypeConverter(typeof(AllFloorsContentsLabelRequiredSignInformationTypeConverter))]
public class AllFloorsContentsLabelRequiredSignInformation
{
    private Color foreColor = Color.Red;
    private ConfirmationAnswers visible = ConfirmationAnswers.Yes;

    public AllFloorsContentsLabelRequiredSignInformation()
    {

    }

    [Category("Appearance")]
    [DisplayName("ForeColor")]
    [Description("ForeColor")]
    [DefaultValue(typeof(Color), "Red")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public new Color ForeColor
    {
        get
        {
            return foreColor;
        }
        set
        {
            foreColor = value;
        }
    }

    [Category("Behavior")]
    [DisplayName("Visible")]
    [Description("Visibleeeeeeeeeeeeeeeeee")]
    [DefaultValue(ConfirmationAnswers.Yes)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public ConfirmationAnswers Visible
    {
        get
        {
            return visible;
        }
        set
        {
            visible = value;
        }
    }
}

public class AllFloorsContentsLabelRequiredSignInformationTypeConverter : ExpandableObjectConverter//TypeConverter
{
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        if (destinationType == typeof(AllFloorsContentsLabelRequiredSignInformation))
        {
            return true;
        }
        return base.CanConvertTo(context, destinationType);
    }

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(String) && value is AllFloorsContentsLabelRequiredSignInformation)
        {
            AllFloorsContentsLabelRequiredSignInformation allFloorsContentsLabelRequiredSignInformation = (AllFloorsContentsLabelRequiredSignInformation)value;
            return allFloorsContentsLabelRequiredSignInformation.ForeColor.ToString() + "; " + allFloorsContentsLabelRequiredSignInformation.Visible.ToString();
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }

    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        if (sourceType == typeof(string))
        {
            return true;
        }
        return base.CanConvertFrom(context, sourceType);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        if (value is string)
        {
            AllFloorsContentsLabelRequiredSignInformation allFloorsContentsLabelRequiredSignInformation = new AllFloorsContentsLabelRequiredSignInformation();
            string strExtractData = (string)value;
            Color clrForeColor = Color.FromName(strExtractData.Substring(0, strExtractData.IndexOf(";") - 1).Trim());
            string strVisible = strExtractData.Substring(strExtractData.IndexOf(";") + 1, strExtractData.Length).Trim();

            allFloorsContentsLabelRequiredSignInformation.ForeColor = clrForeColor;
            if (strVisible == "Yes")
            {
                allFloorsContentsLabelRequiredSignInformation.Visible = ConfirmationAnswers.Yes;
            }
            else
            {
                allFloorsContentsLabelRequiredSignInformation.Visible = ConfirmationAnswers.No;
            }
            return allFloorsContentsLabelRequiredSignInformation;
        }
        return base.ConvertFrom(context, culture, value);
    }
}

Upvotes: 1

Views: 2721

Answers (1)

LarsTech
LarsTech

Reputation: 81610

Your property has only a "Get", so it's read only. Try adding a "Set" property:

[Category("Data")]
[DisplayName("Required Sign")]
[Description("Required Signnnnnnnnnnnnnnn")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public AllFloorsContentsLabelRequiredSignInformation AllFloorsContentsLabelRequiredSignInfo {
  get {
    return allFloorsContentsLabelRequiredSignInformation;
  }
  set {
    allFloorsContentsLabelRequiredSignInformation = value;
  }
}

Your ConvertFrom has issues where it needs to do more error checking.

Upvotes: 2

Related Questions