我有一个在Winforms中开发的表单,并使用PropertyGrid来允许用户编辑数据。我已经想出了如何为需要超过属性网格的内置功能的属性定义自定义类型编辑器,这一切都能成功运行。我有一个String属性,虽然它的类型编辑器打开一个模态对话框,并允许它们通过我定义的表单编辑字符串,但是当它很简单时,这个特殊的属性也可以作为自由格式文本输入到属性网格中。
所以我还想在属性网格中显示省略号按钮,以便他们可以通过对话框进行编辑,但我也希望属性网格中的单元格允许自由格式文本。必须有一种方法可以做到这一点,因为字体类型的编辑器允许这样做,但我还没有找到解决方案。有人知道怎么做吗?谢谢你。
答案 0 :(得分:1)
您可以使用支持string
转化的TypeConverter。例如,这里是一个TypeConverter,用于Encoding类型的属性,它将被声明为:
public class MyClass
{
...
[TypeConverter(typeof(EncodingConverter))]
public Encoding MyEncoding { get; set; }
...
}
/// <summary>
/// Provides a type converter to convert Encoding objects to and from various other representations.
/// </summary>
public class EncodingConverter: TypeConverter
{
/// <summary>
/// Returns whether this converter can convert an object of the given type to the type of this converter, using the specified context.
/// </summary>
/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that provides a format context.</param>
/// <param name="sourceType">A <see cref="T:System.Type"/> that represents the type you want to convert from.</param>
/// <returns>
/// true if this converter can perform the conversion; otherwise, false.
/// </returns>
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return ((typeof(string) == sourceType) || base.CanConvertFrom(context, sourceType));
}
/// <summary>
/// Returns whether this converter can convert the object to the specified type, using the specified context.
/// </summary>
/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that provides a format context.</param>
/// <param name="destinationType">A <see cref="T:System.Type"/> that represents the type you want to convert to.</param>
/// <returns>
/// true if this converter can perform the conversion; otherwise, false.
/// </returns>
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return ((typeof(string) == destinationType) || base.CanConvertTo(context, destinationType));
}
/// <summary>
/// Converts the given object to the type of this converter, using the specified context and culture information.
/// </summary>
/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that provides a format context.</param>
/// <param name="culture">The <see cref="T:System.Globalization.CultureInfo"/> to use as the current culture.</param>
/// <param name="value">The <see cref="T:System.Object"/> to convert.</param>
/// <returns>
/// An <see cref="T:System.Object"/> that represents the converted value.
/// </returns>
/// <exception cref="T:System.NotSupportedException">
/// The conversion cannot be performed.
/// </exception>
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (!(value is string))
return base.ConvertFrom(context, culture, value);
string name = (string)value;
if (name == null)
return null;
return Encoding.GetEncoding(name);
}
/// <summary>
/// Converts the given value object to the specified type, using the specified context and culture information.
/// </summary>
/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that provides a format context.</param>
/// <param name="culture">A <see cref="T:System.Globalization.CultureInfo"/>. If null is passed, the current culture is assumed.</param>
/// <param name="value">The <see cref="T:System.Object"/> to convert.</param>
/// <param name="destinationType">The <see cref="T:System.Type"/> to convert the <paramref name="value"/> parameter to.</param>
/// <returns>
/// An <see cref="T:System.Object"/> that represents the converted value.
/// </returns>
/// <exception cref="T:System.ArgumentNullException">
/// The <paramref name="destinationType"/> parameter is null.
/// </exception>
/// <exception cref="T:System.NotSupportedException">
/// The conversion cannot be performed.
/// </exception>
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if ((typeof(string) == destinationType) && (value is Encoding))
{
Encoding encoding = (Encoding)value;
return encoding.HeaderName;
}
return base.ConvertTo(context, culture, value, destinationType);
}
}