PropertyGridvalidation

我有一个PropertyGrid 。 当我输入一个错误格式的值(即 – 一个整数项的字符串)时,我收到一条错误信息。 如果我单击“确定”,则坏值将一直保持到我更改它为止。 如果单击“取消”,则返回原始值。

我想控制按钮,因此单击“确定”也会将原始值设置回来,而不是像取消按钮那样显示错误值。

我怎样才能做到这一点?

我会加入@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 { ... } 

但是你想要那个错误对话框(在设置/获取属性时出现exception时显示)并且你想要Ok按钮的工作方式与Cancel相同。 为什么?

简短的回答是:你做不到。 属性网格不支持修改它。

但是,如果你觉得心情不好,这里有一些示例代码演示如何使用此对话框。 当然,使用这一切需要您自担风险。

如何使用此实用程序类:

 propertyGrid1.Site = new MySite(propertyGrid1); propertyGrid1.SelectedObject = MyObj; 

实用程序类代码:

 public class MySite : ISite, IUIService { public MySite(PropertyGrid propertyGrid) { PropertyGrid = propertyGrid; } public object GetService(Type serviceType) { if (serviceType == typeof(IUIService)) return this; return null; } // this is part of IUIService public DialogResult ShowDialog(Form form) { // Check the form passed here is the error dialog box. // It's type name should be GridErrorDlg. // You can also scan all controls and for example // remove or modify some buttons... DialogResult result = form.ShowDialog(PropertyGrid); if (form.GetType().Name == "GridErrorDlg" && result == DialogResult.OK) { PropertyGrid.Refresh(); } return result; } public PropertyGrid PropertyGrid { get; private set; } public bool DesignMode { get { return false; } } public IContainer Container { get { return null; } } public bool CanShowComponentEditor(object component) { return false; } // I've left the rest as not implemented, but make sure the whole thing works in your context... public IComponent Component { get { throw new NotImplementedException(); } } public string Name { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } public IWin32Window GetDialogOwnerWindow() { throw new NotImplementedException(); } public void SetUIDirty() { throw new NotImplementedException(); } public bool ShowComponentEditor(object component, IWin32Window parent) { throw new NotImplementedException(); } public void ShowError(Exception ex, string message) { throw new NotImplementedException(); } public void ShowError(Exception ex) { throw new NotImplementedException(); } public void ShowError(string message) { throw new NotImplementedException(); } public DialogResult ShowMessage(string message, string caption, MessageBoxButtons buttons) { throw new NotImplementedException(); } public void ShowMessage(string message, string caption) { throw new NotImplementedException(); } public void ShowMessage(string message) { throw new NotImplementedException(); } public bool ShowToolWindow(Guid toolWindow) { throw new NotImplementedException(); } public System.Collections.IDictionary Styles { get { throw new NotImplementedException(); } } }