ICustomTypeDescriptor 实现时抛出参数异常

发布于 2024-10-15 06:14:30 字数 3352 浏览 2 评论 0原文

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

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

我有什么错吗?

系统参数异常: 无法绑定到数据源上的属性或列 TestNamefür。 参数名称:dataMember 位于 System.Windows.Forms.BindToObject.CheckBinding() bei System.Windows.Forms.Binding.SetListManager(BindingManagerBase bindingManagerBase) bei System.Windows.Forms.ListManagerBindingsCollection.AddCore(绑定 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) 的属性无法绑定!所以我这样做了:

为什么 Browsable 属性使属性不可绑定?

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

I want to exclude the Property MiddleName from the browseable Properties in my PropertyGrid.

When I hang around the interface ICustomTypeDescriptor on my Person class I get this exception while starting my app.

What do I wrong?

System.ArgumentException:
Can not bind to the property or column TestNamefür on the DataSource.
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;
    }
}

UPDATE + SOLUTION:

Properties which are marked with Browseable(false) can not be bound! so I did this:

Why Browsable attribute makes property not bindable?

The last solution from Marc Gravell worked like a breath!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酷到爆炸 2024-10-22 06:14:30

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

无论如何,如果您的唯一目的只是从 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来自属性网格的属性...

I tested your code seems to work in my test. Maybe we need more of your code to understand what's the problem.

Anyway, if your only purpose is just to hide MiddleName property from Propertygrid, why don't simply put [Browsable(false) attribute on that property, instead of implement an ICustomTypeDescriptor?

It would save you from a lot of code...

EDIT:

I mean, a code like this must work:

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

and should correctly hide MiddleName property from property grid...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文