2011-02-01 78 views
1

我想從PropertyGrid中的可瀏覽屬性中排除Property MiddleName。ICustomTypeDescriptor在執行時拋出argumentsmentexception

當我在我的Person類上掛接接口ICustomTypeDescriptor時,在啓動我的應用程序時出現此異常。

我該怎麼做?

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 +溶液:

性能,這是標有Browseable(false)不能綁定!所以我這樣做:

Why Browsable attribute makes property not bindable?

馬克Gravell最後解決方案的工作就像一個呼吸!

回答

0

我測試了你的代碼似乎在我的測試中工作。也許我們需要更多的代碼來了解問題所在。

無論如何,如果你唯一的目的只是爲了從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財產從屬性網格...

+0

我忘了說=>如果我使用額頭在TestName上可以使用attribut我遇到了同樣的異常:/ – Elisabeth 2011-02-01 12:01:43

相關問題