C# PropertyGrid显示文本框内联,而不是下拉
几乎完全符合我的需要,但我不想使用下拉列表,而是希望编辑在网格中进行C# PropertyGrid显示文本框内联,而不是下拉,c#,user-interface,propertygrid,C#,User Interface,Propertygrid,几乎完全符合我的需要,但我不想使用下拉列表,而是希望编辑在网格中进行 UITypeEditorEditStyle似乎没有多大帮助,因为将其设置为“无”将完全消除文本框控件 或者,有没有更简单的方法来访问控件用来钩住事件的文本框 最后,我正在寻找一个文本输入小部件,它将输入的长度限制为1。内联完成,和2。不必等到用户停止键入时才给他们一个错误或截断输入。您可以使用一个类型转换器,一旦输入文本,它将限制文本。只有文本框失去焦点,这才有效。假设我有一个要使用属性网格编辑的类: public clas
UITypeEditorEditStyle
似乎没有多大帮助,因为将其设置为“无”将完全消除文本框控件
或者,有没有更简单的方法来访问控件用来钩住事件的文本框
最后,我正在寻找一个文本输入小部件,它将输入的长度限制为1。内联完成,和2。不必等到用户停止键入时才给他们一个错误或截断输入。您可以使用一个类型转换器,一旦输入文本,它将限制文本。只有文本框失去焦点,这才有效。假设我有一个要使用属性网格编辑的类:
public class MyClass
{
[TypeConverter(typeof(MyTypeConverter))]
public string MyText { get; set; }
}
以下是执行此操作的类型转换器代码:
public class MyTypeConverter : TypeConverter
{
public const int MaxLength = 10;
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return destinationType == typeof(string);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
return Truncate(value as string, MaxLength);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
return Truncate(value as string, MaxLength);
}
private static string Truncate(string value, int maxLength)
{
if (value == null)
return null;
return value.Length <= maxLength ? value : value.Substring(0, maxLength);
}
}
请注意,MyText属性由一个“标记”编辑器装饰,该编辑器什么也不做:
public class NoneEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.None;
}
}
我可以挂上网格,就像这样:
propertyGrid1.SelectedGridItemChanged += OnPropertyGridSelectedGridItemChanged;
public static void OnPropertyGridSelectedGridItemChanged(object sender, SelectedGridItemChangedEventArgs e)
{
PropertyGrid pg = (PropertyGrid)sender;
GridItem item = e.NewSelection;
// yes, a grid item is also an IServiceProvider
IServiceProvider sp = (IServiceProvider)item;
// get the property grid view control
IWindowsFormsEditorService svc = (IWindowsFormsEditorService)sp.GetService(typeof(IWindowsFormsEditorService));
// WARNING: hack time! this uses private members, so use at your own risks...
TextBox edit = (TextBox)svc.GetType().GetProperty("Edit", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(svc, null);
// is this our funky stuff?
if (item.PropertyDescriptor.GetEditor(typeof(UITypeEditor)) is NoneEditor)
{
edit.MaxLength = 10;
edit.BackColor = Color.Blue;
}
else // don't forget to reset the edit box here
{
edit.MaxLength = int.MaxValue;
edit.BackColor = Color.White;
}
}
PropertyGrid是福特T型用户界面设计:“客户可以选择任何颜色,只要是黑色”。至少有3个选项,文本框是默认值,组合框或自定义编辑器的对话框。输入验证需要在类型转换器中进行。最后,我将钩子的一个变体改为
SelectedGridItemChanged
,效果非常好。我一直在尝试子类化属性Grid
。。。最终还是陷入了这样的境地。谢谢
propertyGrid1.SelectedGridItemChanged += OnPropertyGridSelectedGridItemChanged;
public static void OnPropertyGridSelectedGridItemChanged(object sender, SelectedGridItemChangedEventArgs e)
{
PropertyGrid pg = (PropertyGrid)sender;
GridItem item = e.NewSelection;
// yes, a grid item is also an IServiceProvider
IServiceProvider sp = (IServiceProvider)item;
// get the property grid view control
IWindowsFormsEditorService svc = (IWindowsFormsEditorService)sp.GetService(typeof(IWindowsFormsEditorService));
// WARNING: hack time! this uses private members, so use at your own risks...
TextBox edit = (TextBox)svc.GetType().GetProperty("Edit", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(svc, null);
// is this our funky stuff?
if (item.PropertyDescriptor.GetEditor(typeof(UITypeEditor)) is NoneEditor)
{
edit.MaxLength = 10;
edit.BackColor = Color.Blue;
}
else // don't forget to reset the edit box here
{
edit.MaxLength = int.MaxValue;
edit.BackColor = Color.White;
}
}