首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ICustomTypeDescriptor在实现时抛出argumentexception

ICustomTypeDescriptor在实现时抛出argumentexception
EN

Stack Overflow用户
提问于 2011-02-01 10:52:56
回答 1查看 765关注 0票数 1

我希望将属性MiddleName排除在PropertyGrid中的可浏览属性之外。

当我在Person类上的接口ICustomTypeDescriptor上徘徊时,我在启动我的应用程序时得到了这个异常。

我做错什么了?

System.ArgumentException:无法绑定到DataSource上的属性或列TestNamefür。参数名称: System.Windows.Forms.BindToObject.CheckBinding() bei System.Windows.Forms.Binding.SetListManager(BindingManagerBase bindingManagerBase) bei System.Windows.Forms.ListManagerBindingsCollection.AddCore(Binding dataBinding)

代码语言:javascript
复制
public class Person : ICustomTypeDescriptor
{
    public string TestName { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    AttributeCollection ICustomTypeDescriptor.GetAttributes()
    {
      return TypeDescriptor.GetAttributes(this, true);
    }
    string ICustomTypeDescriptor.GetClassName()
    {
      return TypeDescriptor.GetClassName(this, true);
    }
    string ICustomTypeDescriptor.GetComponentName()
    {
      return TypeDescriptor.GetComponentName(this, true);
    }
    TypeConverter ICustomTypeDescriptor.GetConverter()
    {
      return TypeDescriptor.GetConverter(this, true);
    }
    EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
    {
      return TypeDescriptor.GetDefaultEvent(this, true);
    }
    PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
    {
      return TypeDescriptor.GetDefaultProperty(this, true);
    }
    object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
    {
      return TypeDescriptor.GetEditor(this, editorBaseType, true);
    }
    EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
    {
      return TypeDescriptor.GetEvents(this, attributes, true);
    }
    EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
    {
      return TypeDescriptor.GetEvents(this, true);
    }
    PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
    {
      Debug.Print("GetProperties()");
      Print("Attributes is {0}null", attributes == null ? "" : "not ");
      PropertyDescriptorCollection origCol = TypeDescriptor.GetProperties(this, attributes, true);
      bool wantBrowsable = attributes.Contains<Attribute>(new BrowsableAttribute(true));
      Debug.Print("Wants Browsable: {0}", wantBrowsable);
      List<PropertyDescriptor> newCol = new List<PropertyDescriptor>();
      foreach (PropertyDescriptor pd in origCol)
      {
        System.Diagnostics.Debug.Print("Property Name: {0}", pd.Name);
        if (pd.Name != "MiddleName")
        {
          System.Diagnostics.Debug.Print("Property {0} is included.", pd.Name);
          newCol.Add(pd);
        }
      }
      return new PropertyDescriptorCollection(newCol.ToArray());
    }
    PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
    {
      return ((ICustomTypeDescriptor)this).GetProperties(null);
    }
    object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
    {
      return this;
    }
}

更新+解决方案:

标记为Browseable(false)的属性不能绑定!所以我做了这个:

Why Browsable attribute makes property not bindable?

Marc 的最后一个解决方案非常有效!

EN

回答 1

Stack Overflow用户

发布于 2011-02-01 11:58:03

我测试了您的代码,似乎在我的测试中有效。也许我们需要更多的代码来理解问题所在。

无论如何,如果您的唯一目的只是将MiddleName属性隐藏在Propertygrid中,那么为什么不简单地将[Browsable(false)属性放在该属性上,而不是实现一个ICustomTypeDescriptor

这会让你从大量代码中拯救出来..。

编辑:

我是说,这样的代码一定有效:

代码语言:javascript
复制
public class Person
{
    public string TestName { get; set; }

    public string FirstName { get; set; }

    [Browsable(false)]
    public string MiddleName { get; set; }

    public string LastName { get; set; }
}

并正确地将MiddleName属性隐藏在属性网格中.

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4861645

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档