ICustomTypeDescriptor在实现时抛出argumentexception

时间:2011-02-01 10:52:56

标签: c# .net propertygrid icustomtypedescriptor

我想从PropertyGrid中的可浏览属性中排除Property MiddleName。

当我在Person类的界面ICustomTypeDescriptor上闲逛时,我在启动应用程序时遇到此异常。

我错了什么?

  

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

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 Gravell 的最后一个解决方案就像呼吸一样!

1 个答案:

答案 0 :(得分:0)

我测试过你的代码似乎在我的测试中起作用。也许我们需要更多代码才能理解问题所在。

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

它会为你节省很多代码......

修改

我的意思是,像这样的代码必须工作:

ICustomTypeDescriptor

并且应该正确隐藏属性网格中的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; } } 属性...