【问题标题】:ICustomTypeDescriptor throws argumentexception when implementedICustomTypeDescriptor 实现时抛出参数异常
【发布时间】:2011-02-01 10:52:56
【问题描述】:

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

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

我做错了什么?

System.ArgumentException: 无法绑定到 DataSource 上的属性或列 TestNamefür。 参数名称:dataMember 北 System.Windows.Forms.BindToObject.CheckBinding() 北 System.Windows.Forms.Binding.SetListManager(BindingManagerBase bindingManagerBase) 北 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 的最后一个解决方案效果很好!

【问题讨论】:

    标签: c# .net propertygrid icustomtypedescriptor


    【解决方案1】:

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

    无论如何,如果您的唯一目的只是从Propertygrid 中隐藏MiddleName 属性,为什么不简单地将[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; }
    }
    

    并且应该正确地从属性网格中隐藏MiddleName 属性...

    【讨论】:

    • 我忘了说 => 如果我在 TestName 上使用可浏览属性,我会得到相同的异常:/
    • 即使你从 person 类中删除了 ICustomTypeDescriptor 实现?这对我来说真的很奇怪...... Person 类正是你发布的方式吗?
    • @digEmAll 我在哪里通过实现 ICustomTypDescriptor 说我不想让 TestName 作为 DataMember?顺便提一句。这个类只是一个虚拟类,我不能在这里发布真正的代码。(不允许)
    • 好的,我发现了错误:设计器文件中有旧的 DataBinding 但不再使用。我删除了整个绑定源并摆脱了当前的异常。我现在看到的问题是我的 propertyGrid 绑定到 person 对象及其一些属性。同时,我在 propertygrid 之外有控件,这些控件绑定到我想隐藏在 propertygrid 中的属性。有人知道这个的解决方案吗?
    猜你喜欢
    • 2015-06-03
    • 1970-01-01
    • 2013-05-24
    • 2013-03-14
    • 2016-06-21
    • 1970-01-01
    • 2013-02-17
    • 2013-11-18
    • 2023-03-07
    相关资源
    最近更新 更多