一般来说,这个自定义的 PropertyGrid 工作得很好,为我提供了我们所需要的东西。但是,我还没有弄清楚如何显示蒙面密码字符。我希望为密码显示典型的“*”,但保留实际键入的值。
目前,在 IntegratorPropertyDescriptor-> PropertyType 中,我将密码设置为 typeof(PasswordPropertyTextAttribute),这使得属性不可键入。我之前将它作为 typeof(string),所以我们仍然可以获得密码,但它是可读的。
我已经检查了 PropertyDescriptor 的所有覆盖属性,但没有看到任何可用于密码的内容。
如果其他人解决了这个问题,我将不胜感激。谢谢!
public class IntegratorPropertyGridAdapter : ICustomTypeDescriptor
{
IDictionary _dictionary;
public IntegratorPropertyGridAdapter(IDictionary d)
{
_dictionary = d;
}
#region Not Used
public string GetComponentName() //Never used but needs to be here
{
return TypeDescriptor.GetComponentName(this, true);
}
public EventDescriptor GetDefaultEvent() //Never used but needs to be here
{
return TypeDescriptor.GetDefaultEvent(this, true);
}
public string GetClassName() //Never used but needs to be here
{
return TypeDescriptor.GetClassName(this, true);
}
#endregion
public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this, attributes, true);
}
EventDescriptorCollection System.ComponentModel.ICustomTypeDescriptor.GetEvents()
{
return TypeDescriptor.GetEvents(this, true);
}
public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(this, true);
}
public object GetPropertyOwner(PropertyDescriptor pd)
{
return _dictionary;
}
public object GetPropertyOwner()
{
return _dictionary;
}
public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(this, true);
}
public object GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType, true);
}
public PropertyDescriptor GetDefaultProperty()
{
return null;
}
PropertyDescriptorCollection
ICustomTypeDescriptor.GetProperties()
{
return ((ICustomTypeDescriptor) this).GetProperties(new Attribute[0]);
}
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
var properties = new ArrayList();
foreach (DictionaryEntry e in _dictionary)
{
properties.Add(new IntegratorPropertyDescriptor(_dictionary, e.Key));
}
var props = (PropertyDescriptor[]) properties.ToArray(typeof(PropertyDescriptor));
return new PropertyDescriptorCollection(props);
}
}
private class IntegratorPropertyDescriptor : PropertyDescriptor
{
IDictionary _dictionary;
object _key;
public IntegratorPropertyDescriptor(IDictionary d, object key)
: base(((IntegratorSetup) key).Prompt, null)
{
_dictionary = d;
_key = key;
}
public override string Description => ((IntegratorSetup) _key).Help;
public override string Category => ((IntegratorSetup) _key).Group;
public override Type PropertyType
{
get
{
try
{
if (((IntegratorSetup) _key)?.Type == null)
return typeof(string);
switch (((IntegratorSetup) _key).Type.ToLower())
{
case "text":
return typeof(string);
case "password":
return typeof(PasswordPropertyTextAttribute); //Previously typeof(string)
case "int":
return typeof(int);
case "bool":
return typeof(bool);
case "array":
return typeof(string[]);
case "list":
return typeof(ITypedList);
default:
return typeof(string);
}
}
catch
{
return typeof(string);
}
}
}
public override void SetValue(object component, object value)
{
((IntegratorSetup) _key).Inputted = value;
}
public override object GetValue(object component)
{
return ((IntegratorSetup) _key).Inputted;
}
public override bool IsReadOnly => ((IntegratorSetup) _key).ReadOnly;
public override Type ComponentType => Type.GetType(((IntegratorSetup) _key).Type);
public override bool CanResetValue(object component)
{
return true;
}
public override void ResetValue(object component)
{
}
public override bool ShouldSerializeValue(object component)
{
return false;
}
}