2012-10-17 42 views
1

我创建了一个具有多个属性的自定义控件。我为自定义控件添加了一些可扩展属性。现在,我希望该用户可以编辑属性网格中的可扩展属性字段以及为其相关属性设置的新输入值。我在属性网格扩张属性“需要登录”,并有两个子属性如下:如何在属性网格中编辑可扩展属性的字段?

  1. 前景色

  2. 可见

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

"Required Sign" expandable property

  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); 
    } 
} 
+0

你可以编辑集合编辑器的其他属性吗? – LightStriker

+0

嗨马克安德烈Jutras。是的,我可以编辑其他属性的字段,除了我的可扩展属性(必填标志)的字段外。 – MRS1367

回答

2

你的财产只有一个“获取”,所以它是只读的。尝试添加“设置”属性:

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

ConvertFrom有问题,它需要做更多的错误检查。

+0

哦。你是对的。为什么我忘记这个主题! Tnx为您的答案。 – MRS1367

+0

@ MRS1367你终于做到了吗?我用你的代码来达到类似的目的,但是在属性中设置的数据不会持续或持续。希望你能理解我脑海中的问题或困境。 –

+0

@PrakashVishwakarma - >进入C#聊天室。我们可以在那里谈论它。 – MRS1367