Here's what I've done in the past. It displays "********" for the password in the grid, with a "..." button to allow the user to set the password (using a dialog that you supply).
public class User
{
[TypeConverter(typeof(PasswordConverter))]
[Editor(typeof(PasswordEditor), typeof(UITypeEditor))]
public string Password { get; set; }
}
public class PasswordConverter : TypeConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string)) return true;
return base.CanConvertTo(context, destinationType);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
string password = (string)value;
if (password != null && password.Length > 0)
{
return "********";
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
public class PasswordEditor : UITypeEditor
{
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
string password = (string)value;
// Show a dialog allowing the user to enter a password
return password;
}
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
}
answered 2009-01-14T15:58:54.050