2011-02-01 3 views
1

내 PropertyGrid의 탐색 가능한 속성에서 속성 중간 이름을 제외하려고합니다.ICustomTypeDescriptor 구현시 인수 예외가 throw됩니다.

내 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)로 표시됩니다 바인딩 할 수 없습니다! 마크 Gravell에서 마지막 솔루션은 호흡처럼 일

Why Browsable attribute makes property not bindable?

: 그래서 이런 짓을!

답변

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

=> 나는 이마를 사용한다면 => seable attribut에 대한 TestName 동일한 예외 :/ – Elisabeth

+0

사람 클래스에서'ICustomTypeDescriptor' 구현을 제거하더라도? 정말 이상하게 보입니다 ... Person 클래스가 정확히 어떻게 게시 했습니까? – digEmAll

+0

@Lisa : 내 편집을 확인하십시오.) – digEmAll

관련 문제