【问题标题】:How can edit field of an Expandable Property in the property grid?如何在属性网格中编辑可扩展属性的字段?
【发布时间】:2012-10-17 21:43:24
【问题描述】:

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

  1. 前景色

  2. 可见

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

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

【问题讨论】:

  • 您可以从集合编辑器编辑其他属性吗?
  • 嗨,Marc-André Jutras。是的,我可以编辑其他属性的字段,但我的可扩展属性的字段(必填)除外。

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


【解决方案1】:

您的属性只有一个“Get”,因此它是只读的。尝试添加“Set”属性:

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

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

【讨论】:

  • 哦。你是对的。为什么我忘记了这个话题! Tnx 为您解答。
  • @MRS1367 你终于成功了吗?我将您的代码用于类似目的,但属性中的数据集无法维持或持续存在。希望你能理解我心中的问题或困境。
  • @PrakashVishwakarma -> 来 C# 聊天室吧。我们可以在那里讨论。
  • @PrakashVishwakarma -> 如果你想和我谈谈你的问题,你可以在 C# 聊天室找到我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多