我将加入 @Crono 的行列,你为什么想要你想要的?
如果你问我怎样才能删除那个对话框,那么我可以回答使用自己的TypeConverter:
public class IntConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return true;
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return true;
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if(value is string)
{
// try parse to int, do not throw exception
}
return 0; // always return something
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
return value.ToString();
return base.ConvertTo(context, culture, value, destinationType); // i left it here but it should never call it
}
}
如果你问我想要我自己的对话框来编辑一些东西,那么我会回答使用自己的 UITypeEditor:
public class MyEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
Form1 form1 = new Form1();
form1.ShowDialog();
return form1.SomeProperty;
}
}
而用法是
[TypeConverter(typeof(IntConverter))]
[EditorAttribute(typeof(MyEditor), typeof(UITypeEditor))]
public int SomeProperty
{
...
}
但是您想要那个错误对话框(当设置/获取属性时出现异常时显示)并且您希望 Ok 按钮的工作方式与 Cancel 相同。为什么?