2017-03-08 22 views
1

如果我將對象分配給PropertyGridControl.SelectedObjuect then I can use [PasswordPropertyTextAttribute(true)] for the password property並且效果很好。如何在DevExpress PropertyGridControl中使用密碼字符

但是我通過實現ICustomTypeDescriptor如下,這沒有任何影響的對象:

AttributeCollection ICustomTypeDescriptor.GetAttributes() 
{ 
    AttributeCollection attrColl = TypeDescriptor.GetAttributes(this, true); 
    Attribute [] attrs = new Attribute[attrColl.Count + 1]; 
    attrColl.CopyTo(attrs, 0); 
    attrs[attrs.Length-1] = new PasswordPropertyTextAttribute(true); 
    return new AttributeCollection(attrs); 
} 

有沒有辦法做到這一點?我們正在使用Windows Forms & C#。

+0

相關:http://stackoverflow.com/questions/11892064/propertygrid-with-custom-propertydescriptor –

回答

0

事實是,一個不正確的方法用於實現所需的結果。如ICustomTypeDescriptor.GetAttributes MSDN文章中所述,此方法爲組件的此實例返回一組自定義屬性。 但是,要將屬性分配給目標對象的屬性,您需要重寫ICustomTypeDescriptor.GetProperties方法。在特定情況下,可以實現以下方式方法:

PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes) 
     { 
      PropertyDescriptorCollection props = TypeDescriptor.GetProperties(this, attributes, true); 
      return UpdateProperties(props); 
     } 
     PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties() 
     { 
      PropertyDescriptorCollection props = TypeDescriptor.GetProperties(this, true); 
      return UpdateProperties(props); 
     } 
     private PropertyDescriptorCollection UpdateProperties(PropertyDescriptorCollection props) 
     { 
      List<PropertyDescriptor> newProps = new List<PropertyDescriptor>(); 
      PropertyDescriptor current; 
      foreach (PropertyDescriptor prop in props) 
      { 
       current = prop; 
       if (prop.Name == "UserPassword") 
        current = TypeDescriptor.CreateProperty(typeof(UserInfo), prop, new Attribute[] { new PasswordPropertyTextAttribute(true) }); 
       newProps.Add(current); 
      } 
      return new PropertyDescriptorCollection(newProps.ToArray()); ; 
     } 
相關問題