如何在属性网格中编辑可扩展属性的字段?

时间:2012-10-17 12:19:35

标签: c# winforms custom-controls propertygrid design-time

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

  1. 前景色

  2. 可见

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

    "Required Sign" expandable property

    1. 绿箱:“必填符号”可扩展物业

    2. Blue Box:“必需符号”可扩展属性的两个子属性

    3. 红色框:“必需符号”可扩展属性的字段

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

      我的代码如下:

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

1 个答案:

答案 0 :(得分:2)

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

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

您的ConvertFrom存在需要进行更多错误检查的问题。