1

我创建了一个具有多个属性的自定义控件。我向我的自定义控件添加了一些可扩展的属性。现在,我希望用户可以编辑属性网格中的可扩展属性字段,并为其相关属性设置新的输入值。我在属性网格中的可扩展属性是“必需标志”,并且有两个子属性,如下所示:

  1. 前景色

  2. 可见的

我将“Required Sign”可扩展属性的两个子属性的值设置为“Required Sign”属性的字段,如下图所示:

  1. 绿框:“必需标志”可扩展属性

  2. 蓝框:“必需标志”可扩展属性的两个子属性

  3. 红框:“必需标志”可扩展属性的字段

但是,我无法直接更改或编辑“必填符号”可扩展属性的字段值。如何更改或编辑可扩展属性(图中红框)的字段值?

我的代码如下:

[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);
    }
}
4

1 回答 1

2

您的属性只有一个“获取”,因此它是只读的。尝试添加“设置”属性:

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

ConvertFrom有需要进行更多错误检查的问题。

于 2012-10-17T13:43:24.993 回答