【问题标题】:How to set properties of a custom UITypeEditor using Attributes如何使用 Attributes 设置自定义 UITypeEditor 的属性
【发布时间】:2014-10-24 08:05:17
【问题描述】:

出于好奇,想象一下你有这样一个 UITypeEditor:

public class CustomEditor : System.Drawing.Design.UITypeEditor
{
    public bool DoSomething { get; set; }
    [...]
}

并且您想使用它来编辑您的属性之一,并将DoSomething 设置为true

public MyClass
{
    [EditorAttribute(typeof(CustomEditor), typeof(System.Drawing.Design.UITypeEditor))]
    public string MyProperty { get; set; }
    [...]
}

如何在实例化编辑器时为CustomEditorDoSomething 属性指定一个值? 这是否可能,还是您必须创建与可能配置数量一样多的继承CustomEditor 的类?

【问题讨论】:

    标签: c# .net uitypeeditor


    【解决方案1】:

    UITypeEditor.EditValue 的实现中,您可以查看context 参数以获取对正在编辑的属性的描述符的引用。然后,您可以查看 另一个 属性,您可以在其中放置编辑器配置值。

    public class CustomEditor : System.Drawing.Design.UITypeEditor
    {
    
       public override object EditValue(
           ITypeDescriptorContext context,
           IServiceProvider provider,
           object value)
       {
           var property = context.PropertyDescriptor;
           var config = (MyConfigAttribute)
               property.Attributes[typeof(MyConfigAttribute)];
           // ...
       }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-02
      • 2017-06-12
      • 1970-01-01
      • 2010-10-25
      • 1970-01-01
      • 2013-01-25
      相关资源
      最近更新 更多