C# PropertyGrid控件应用心得

时间:2022-05-14 03:08:03

何处使用 PropertyGrid 控件
在应用程序中的很多地方,您都可以使用户与 PropertyGrid 进行交互,从而获得更丰富的编辑体验。例如,某个应用程序包含多个用户可以设置的“设置”或选项,其中一些可能十分复杂。您可以使用单选按钮、组合框或文本框来表示这些选项。但本文将逐步介绍如何使用 PropertyGrid 控件创建选项窗口来设置应用程序选项。上面所创建的 OptionsDialog 窗体即是选项窗口的开始。现在,我们创建一个名为 AppSettings 的类,其中包含映射到应用程序设置的所有属性。如果创建单独的类而不使用多个分散的变量,设置将更便于管理和维护。

  1. public class AppSettings{
  2. private bool saveOnClose = true;
  3. private string greetingText = "欢迎使用应用程序!";
  4. private int itemsInMRU = 4;
  5. private int maxRepeatRate = 10;
  6. private bool settingsChanged = false;
  7. private string appVersion = "1.0";
  8. public bool SaveOnClose
  9. {
  10. get { return saveOnClose; }
  11. set { saveOnClose = value;}
  12. }
  13. public string GreetingText
  14. {
  15. get { return greetingText; }
  16. set { greetingText = value; }
  17. }
  18. public int MaxRepeatRate
  19. {
  20. get { return maxRepeatRate; }
  21. set { maxRepeatRate = value; }
  22. }
  23. public int ItemsInMRUList
  24. {
  25. get { return itemsInMRU; }
  26. set { itemsInMRU = value; }
  27. }
  28. public bool SettingsChanged
  29. {
  30. get { return settingsChanged; }
  31. set { settingsChanged = value; }
  32. }
  33. public string AppVersion
  34. {
  35. get { return appVersion; }
  36. set { appVersion = value; }
  37. }
  38. }

选项窗口上的 PropertyGrid 将使用此类,因此请将类定义添加到应用程序项目中,在添加时可创建新文件或将其添加到现有窗体源代码的下方。

选择对象

要标识 PropertyGrid 显示的内容,请将 PropertyGrid.SelectedObject 属性设置为一个对象实例。然后,PropertyGrid 将完成其余的工作。每次设置 SelectedObject 时,PropertyGrid 都会刷新显示的属性。这提供了一种简单的方法来强制刷新属性,或在运行时切换对象。您还可以调用 PropertyGrid.Refresh 方法来刷新属性。

接下来,您需要更新 OptionsDialog 构造函数中的代码,以创建一个 AppSettings 对象,并将其设置为 PropertyGrid.SelectedObject 属性的值。

  1. public OptionsDialog()
  2. {
  3. OptionsPropertyGrid = new PropertyGrid();
  4. OptionsPropertyGrid.Size = new Size(300, 250);
  5. this.Controls.Add(OptionsPropertyGrid);
  6. this.Text = "选项对话框";
  7. // 创建 AppSettings 类并在 PropertyGrid 中显示该类。
  8. AppSettings appset = new AppSettings();
  9. OptionsPropertyGrid.SelectedObject = appset;
  10. }

编译并运行该应用程序。下面的屏幕快照显示了应用程序的外观。

图 2:PropertyGrid 中选定的 AppSettings 类

自定义 PropertyGrid 控件

您可以修改 PropertyGrid 的某些外观特征以满足自己的需要。可以更改某些属性的显示方式,甚至选择不显示某些属性。那么,如何对 PropertyGrid 进行自定义呢?

更改 PropertyGrid 的外观特征

PropertyGrid 的许多外观特征都可以自定义。下面列出了其中的一部分:

  • 通过 HelpBackColorHelpForeColorHelpVisible 属性可以更改背景颜色、更改字体颜色或隐藏说明窗格。
  • 通过 ToolbarVisible 属性可以隐藏工具栏,通过 BackColor 属性可以更改工具栏的颜色,通过 LargeButtons 属性可以显示大工具栏按钮。
  • 使用 PropertySort 属性可以按字母顺序对属性进行排序和分类。
  • 通过 BackColor 属性可以更改拆分器的颜色。
  • 通过 LineColor 属性可以更改网格线和边框。

本示例中的选项窗口不需要工具栏,因此可以将 ToolbarVisible 设置为 false 。其余属性均保留默认设置。

更改属性的显示方式
要更改某些属性的显示方式,您可以对这些属性应用不同的特性。特性是用于为类型、字段、方法和属性等编程元素添加批注的声明标记,在运行时可以使用反射对其进行检索。下面列出了其中的一部分:

  • DescriptionAttribute - 设置显示在属性下方说明帮助窗格中的属性文本。这是一种为活动属性(即具有焦点的属性)提供帮助文本的有效方法。可以将此特性应用于 MaxRepeatRate 属性。
  • CategoryAttribute - 设置属性在网格中所属的类别。当您需要将属性按类别名称分组时,此特性非常有用。如果没有为属性指定类别,该属性将被分配给杂项 类别。可以将此特性应用于所有属性。
  • BrowsableAttribute – 表示是否在网格中显示属性。此特性可用于在网格中隐藏属性。默认情况下,公共属性始终显示在网格中。可以将此特性应用于 SettingsChanged 属性。
  • ReadOnlyAttribute – 表示属性是否为只读。此特性可用于禁止在网格中编辑属性。默认情况下,带有 get 和 set 访问函数的公共属性在网格中是可以编辑的。可以将此特性应用于 AppVersion 属性。
  • DefaultValueAttribute – 表示属性的默认值。如果希望为属性提供默认值,然后确定该属性值是否与默认值相同,则可使用此特性。可以将此特性应用于所有属性。
  • DefaultPropertyAttribute – 表示类的默认属性。在网格中选择某个类时,将首先突出显示该类的默认属性。可以将此特性应用于 AppSettings 类。

现在,我们将其中的一些特性应用于 AppSettings 类,以更改属性在 PropertyGrid 中的显示方式。

  1. [DefaultPropertyAttribute("SaveOnClose")]
  2. public class AppSettings{
  3. private bool saveOnClose = true;
  4. private string greetingText = "欢迎使用应用程序!";
  5. private int maxRepeatRate = 10;
  6. private int itemsInMRU = 4;
  7. private bool settingsChanged = false;
  8. private string appVersion = "1.0";
  9. [CategoryAttribute("文档设置"),
  10. DefaultValueAttribute(true)]
  11. public bool SaveOnClose
  12. {
  13. get { return saveOnClose; }
  14. set { saveOnClose = value;}
  15. }
  16. [CategoryAttribute("全局设置"),
  17. ReadOnlyAttribute(true),
  18. DefaultValueAttribute("欢迎使用应用程序!")]
  19. public string GreetingText
  20. {
  21. get { return greetingText; }
  22. set { greetingText = value; }
  23. }
  24. [CategoryAttribute("全局设置"),
  25. DefaultValueAttribute(4)]
  26. public int ItemsInMRUList
  27. {
  28. get { return itemsInMRU; }
  29. set { itemsInMRU = value; }
  30. }
  31. [DescriptionAttribute("以毫秒表示的文本重复率。"),
  32. CategoryAttribute("全局设置"),
  33. DefaultValueAttribute(10)]
  34. public int MaxRepeatRate
  35. {
  36. get { return maxRepeatRate; }
  37. set { maxRepeatRate = value; }
  38. }
  39. [BrowsableAttribute(false),
  40. DefaultValueAttribute(false)]
  41. public bool SettingsChanged
  42. {
  43. get { return settingsChanged; }
  44. set { settingsChanged = value; }
  45. }
  46. [CategoryAttribute("版本"),
  47. DefaultValueAttribute("1.0"),
  48. ReadOnlyAttribute(true)]
  49. public string AppVersion
  50. {
  51. get { return appVersion; }
  52. set { appVersion = value; }
  53. }
  54. }

将这些特性应用于 AppSettings 类后,编译并运行该应用程序。下面的屏幕快照显示了应用程序的外观。

图 3:PropertyGrid 中显示的带有类别和默认值的属性

使用此版本的选项窗口后,您会注意到以下几点:

  • 显示窗口时,将首先突出显示 SaveOnClose 属性。
  • 选中 MaxRepeatRate 属性时,说明帮助窗格中将显示“以毫秒表示的文本重复率”。
  • SaveOnClose 属性显示在“文档设置”类别下。其他属性分别显示在“全局设置”和“版本”类别下。
  • SettingsChanged 属性将不再显示。
  • AppVersion 属性为只读。只读属性以灰显文本显示。
  • 如果 SaveOnClose 属性包含的值不是 true ,该值将以粗体显示。PropertyGrid 使用粗体文本表示包含非默认值的属性。

显示复杂属性

到现在为止,选项窗口显示的都是简单的类型,如整数、布尔值和字符串。那么,如何显示更复杂的类型呢?如果应用程序需要跟踪窗口大小、文档字体或工具栏颜色等信息,该如何处理呢?.NET 框架提供的某些数据类型具有特殊的显示功能,能使这些类型在 PropertyGrid 中更具可用性。

对所提供类型的支持
首先,请更新 AppSettings 类,为窗口大小(Size 类型)、窗口字体(Font 类型)和工具栏颜色(Color 类型)添加新属性。

  1. [DefaultPropertyAttribute("SaveOnClose")]
  2. public class AppSettings{
  3. private bool saveOnClose = true;
  4. private string greetingText = "欢迎使用应用程序!";
  5. private int maxRepeatRate = 10;
  6. private int itemsInMRU = 4;
  7. private bool settingsChanged = false;
  8. private string appVersion = "1.0";
  9. private Size windowSize = new Size(100,100);
  10. private Font windowFont = new Font("宋体", 9, FontStyle.Regular);
  11. private Color toolbarColor = SystemColors.Control;
  12. [CategoryAttribute("文档设置"),
  13. DefaultValueAttribute(true)]
  14. public bool SaveOnClose
  15. {
  16. get { return saveOnClose; }
  17. set { saveOnClose = value;}
  18. }
  19. [CategoryAttribute("文档设置")]
  20. public Size WindowSize
  21. {
  22. get { return windowSize; }
  23. set { windowSize = value;}
  24. }
  25. [CategoryAttribute("文档设置")]
  26. public Font WindowFont
  27. {
  28. get {return windowFont; }
  29. set { windowFont = value;}
  30. }
  31. [CategoryAttribute("全局设置")]
  32. public Color ToolbarColor
  33. {
  34. get { return toolbarColor; }
  35. set { toolbarColor = value; }
  36. }
  37. [CategoryAttribute("全局设置"),
  38. ReadOnlyAttribute(true),
  39. DefaultValueAttribute("欢迎使用应用程序!")]
  40. public string GreetingText
  41. {
  42. get { return greetingText; }
  43. set { greetingText = value; }
  44. }
  45. [CategoryAttribute("全局设置"),
  46. DefaultValueAttribute(4)]
  47. public int ItemsInMRUList
  48. {
  49. get { return itemsInMRU; }
  50. set { itemsInMRU = value; }
  51. }
  52. [DescriptionAttribute("以毫秒表示的文本重复率。"),
  53. CategoryAttribute("全局设置"),
  54. DefaultValueAttribute(10)]
  55. public int MaxRepeatRate
  56. {
  57. get { return maxRepeatRate; }
  58. set { maxRepeatRate = value; }
  59. }
  60. [BrowsableAttribute(false),
  61. DefaultValueAttribute(false)]
  62. public bool SettingsChanged
  63. {
  64. get { return settingsChanged; }
  65. set { settingsChanged = value; }
  66. }
  67. [CategoryAttribute("版本"),
  68. DefaultValueAttribute("1.0"),
  69. ReadOnlyAttribute(true)]
  70. public string AppVersion
  71. {
  72. get { return appVersion; }
  73. set { appVersion = value; }
  74. }
  75. }

下面的屏幕快照显示了新属性在 PropertyGrid 中的外观。

图 4:显示在 PropertyGrid 中的 .NET 框架数据类型

请注意,WindowFont 属性带有一个省略号 (...) 按钮,按下该按钮将显示字体选择对话框。此外,还可以展开该属性以显示更多的 Font 属性。某些 Font 属性提供有关字体的值和详细信息的下拉列表。您可以展开 WindowSize 属性以显示 Size 类型的更多属性。最后,请注意,ToolbarColor 属性包含一个选定颜色的样本,以及一个用于选择不同颜色的自定义下拉列表。对于这些以及其他数据类型,.NET 框架提供了其他的类,可以使在 PropertyGrid 中的编辑更加容易。

对自定义类型的支持

现在,您需要在 AppSettings 类中添加另外两个属性,即 DefaultFileName SpellCheckOptions DefaultFileName 属性用于获取或设置字符串;SpellCheckOptions 属性用于获取或设置 SpellingOptions 类的实例。

SpellingOptions 类是一个新类,用于管理应用程序的拼写检查属性。对于何时创建单独的类以管理对象的属性,并没有严格的规定,而取决于您的整个类设计。将 SpellingOptions 类定义添加到应用程序项目中 - 可以添加到新文件中,也可以添加到窗体源代码的下方。

  1. [DescriptionAttribute("展开以查看应用程序的拼写选项。")]
  2. public class SpellingOptions{
  3. private bool spellCheckWhileTyping = true;
  4. private bool spellCheckCAPS = false;
  5. private bool suggestCorrections = true;
  6. [DefaultValueAttribute(true)]
  7. public bool SpellCheckWhileTyping
  8. {
  9. get { return spellCheckWhileTyping; }
  10. set { spellCheckWhileTyping = value; }
  11. }
  12. [DefaultValueAttribute(false)]
  13. public bool SpellCheckCAPS
  14. {
  15. get { return spellCheckCAPS; }
  16. set { spellCheckCAPS = value; }
  17. }
  18. [DefaultValueAttribute(true)]
  19. public bool SuggestCorrections
  20. {
  21. get { return suggestCorrections; }
  22. set { suggestCorrections = value; }
  23. }
  24. }

再次编译并运行选项窗口应用程序。下面的屏幕快照显示了应用程序的外观。

图 5:在 PropertyGrid 中显示的不带类型转换器的自定义数据类型

请注意 SpellcheckOptions 属性的外观。与 .NET 框架类型不同,它不展开或显示自定义的字符串表示。如果要在自己的复杂类型中提供与 .NET 框架类型相同的编辑体验,该如何处理呢?.NET 框架类型使用 TypeConverterUITypeEditor 类提供大部分 PropertyGrid 编辑支持,您也可以使用这些类。

添加可展开属性支持

要使 PropertyGrid 能够展开 SpellingOptions 属性,您需要创建 TypeConverterTypeConverter 提供了从一种类型转换为另一种类型的方法。PropertyGrid 使用 TypeConverter 将对象类型转换为 String ,并使用该 String 在网格中显示对象值。在编辑过程中,TypeConverter 会将 String 转换回对象类型。.NET 框架提供的 ExpandableObjectConverter 类可以简化这一过程。

提供可展开对象支持

  1. 创建一个从 ExpandableObjectConverter 继承而来的类。
    1. public class SpellingOptionsConverter:ExpandableObjectConverter
    2. {   }
  2. 如果 destinationType 参数与使用此类型转换器的类(示例中的 SpellingOptions 类)的类型相同,则覆盖 CanConvertTo 方法并返回 true ;否则返回基类 CanConvertTo 方法的值。
    1. public override bool CanConvertTo(ITypeDescriptorContext context,
    2. System.Type destinationType)
    3. {
    4. if (destinationType == typeof(SpellingOptions))
    5. return true;
    6. return base.CanConvertTo(context, destinationType);
    7. }
  3. 覆盖 ConvertTo 方法,并确保 destinationType 参数是一个 String ,并且值的类型与使用此类型转换器的类(示例中的 SpellingOptions 类)相同。如果其中任一情况为 false ,都将返回基类 ConvertTo 方法的值;否则,返回值对象的字符串表示。字符串表示需要使用唯一分隔符将类的每个属性隔开。由于整个字符串都将显示在 PropertyGrid 中,因此需要选择一个不会影响可读性的分隔符,逗号的效果通常比较好。
    1. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture,
    2. object value, System.Type destinationType)
    3. {
    4. if (destinationType == typeof(System.String) &&
    5. value is SpellingOptions){
    6. SpellingOptions so = (SpellingOptions)value;
    7. return "在键入时检查:" + so.SpellCheckWhileTyping +
    8. ",检查大小写: " + so.SpellCheckCAPS +
    9. ",建议更正: " + so.SuggestCorrections;
    10. }
    11. return base.ConvertTo(context, culture, value, destinationType);
    12. }
  4. (可选)通过指定类型转换器可以从字符串进行转换,您可以启用网格中对象字符串表示的编辑。要执行此操作,首先需要覆盖 CanConvertFrom 方法并返回 true (如果源 Type 参数为 String 类型);否则,返回基类 CanConvertFrom 方法的值。
    1. public override bool CanConvertFrom(ITypeDescriptorContext context, System.Type sourceType)
    2. {
    3. if (sourceType == typeof(string))
    4. return true;
    5. return base.CanConvertFrom(context, sourceType);
    6. }
  5. 要启用对象基类的编辑,同样需要覆盖 ConvertFrom 方法并确保值参数是一个 String 。如果不是 String ,将返回基类 ConvertFrom 方法的值;否则,返回基于值参数的类(示例中的 SpellingOptions 类)的新实例。您需要根据值参数解析类的每个属性的值。了解在 ConvertTo 方法中创建的分隔字符串的格式将有助于您的解析。
    1. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    2. {
    3. if (value is string) {
    4. try {
    5. string s = (string) value;
    6. int colon = s.IndexOf(':');
    7. int comma = s.IndexOf(',');
    8. if (colon != -1 && comma != -1) {
    9. string checkWhileTyping = s.Substring(colon + 1 , (comma - colon - 1));
    10. colon = s.IndexOf(':', comma + 1);
    11. comma = s.IndexOf(',', comma + 1);
    12. string checkCaps = s.Substring(colon + 1 , (comma - colon -1));
    13. colon = s.IndexOf(':', comma + 1);
    14. string suggCorr = s.Substring(colon + 1);
    15. SpellingOptions so = new SpellingOptions();
    16. so.SpellCheckWhileTyping =Boolean.Parse(checkWhileTyping);
    17. so.SpellCheckCAPS = Boolean.Parse(checkCaps);
    18. so.SuggestCorrections = Boolean.Parse(suggCorr);
    19. return so;
    20. }
    21. }
    22. catch {
    23. throw new ArgumentException(
    24. "无法将“" + (string)value +
    25. "”转换为 SpellingOptions 类型");
    26. }
    27. }
    28. return base.ConvertFrom(context, culture, value);
    29. }
  6. 现在已经有了一个类型转换器类,下面您需要确定使用该类的目标类。您可以通过将 TypeConverterAttribute 应用到目标类(示例中的 SpellingOptions 类)来执行此操作。
    1. // 应用于 SpellingOptions 类的 TypeConverter 特性。
    2. [TypeConverterAttribute(typeof(SpellingOptionsConverter)),
    3. DescriptionAttribute(“展开以查看应用程序的拼写选项。")]
    4. public class SpellingOptions{ ... }

再次编译并运行选项窗口应用程序。下面的屏幕快照显示了选项窗口目前的外观。

图 6:在 PropertyGrid 中显示的带有类型转换器的自定义数据类型

注意: 如果只需要可展开对象支持,而不需要自定义字符串表示,则只需将 TypeConverterAttribute 应用到类中。将 ExpandableObjectConverter 指定为类型转换器类型。

添加域列表和简单的下拉列表属性支持

对于基于 Enum 类型返回枚举的属性,PropertyGrid 会自动在下拉列表中显示枚举值。EnumConverter 也提供了这一功能。对于自己的属性,您可能希望为用户提供一个有效值列表(有时也称为选取列表或域列表),而其类型并不是基于 Enum 。如果域值在运行时之前未知,或者值可以更改,则属于这种情况。

修改选项窗口,提供一个用户可从中选择的默认文件名的域列表。您已经将 DefaultFileName 属性添加到 AppSettings 类。下一步是在 PropertyGrid 中显示属性的下拉列表,以提供域列表。

提供简单的下拉列表属性支持

  1. 创建一个从类型转换器类继承而来的类。由于 DefaultFileName 属性属于 String 类型,因此可以从 StringConverter 中继承。如果属性类型的类型转换器不存在,则可以从 TypeConverter 继承;这里并不需要。
    1. public class FileNameConverter: StringConverter {   }
  2. 覆盖 GetStandardValuesSupported 方法并返回 true ,表示此对象支持可以从列表中选取的一组标准值。  
    1. public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
    2. {
    3. return true;
    4. }
  3. 覆盖 GetStandardValues 方法并返回填充了标准值的 StandardValuesCollection 。创建 StandardValuesCollection 的方法之一是在构造函数中提供一个值数组。对于选项窗口应用程序,您可以使用填充了建议的默认文件名的 String 数组。  
    1. public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    2. {
    3. return new StandardValuesCollection(new string[]{"新文件", "文件1", "文档1"});
    4. }
  4. (可选)如果希望用户能够键入下拉列表中没有包含的值,请覆盖 GetStandardValuesExclusive 方法并返回 false 。这从根本上将下拉列表样式变成了组合框样式。
    1. public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
    2. {
    3. return false;
    4. }
  5. 拥有自己的用于显示下拉列表的类型转换器类后,您需要确定使用该类的目标。在本示例中,目标为 DefaultFileName 属性,因为类型转换器是针对该属性的。将 TypeConverterAttribute 应用到目标属性中。
    1. // 应用到 DefaultFileName 属性的 TypeConverter 特性。
    2. [TypeConverter(typeof(FileNameConverter)),
    3. CategoryAttribute("文档设置")]
    4. public string DefaultFileName
    5. {
    6. get{ return defaultFileName; }
    7. set{ defaultFileName = value; }
    8. }

再次编译并运行选项窗口应用程序。下面的屏幕快照显示了选项窗口目前的外观。请注意 DefaultFileName 属性的外观。

图 7:在 PropertyGrid 中显示下拉域列表

 

为属性提供自定义 UI

如上所述,.NET 框架类型使用 TypeConverter UITypeEditor 类(以及其他类)来提供 PropertyGrid 编辑支持。有关如何使用 TypeConverter ,请参阅对自定义类型的支持一节;您也可以使用 UITypeEditor 类来自定义 PropertyGrid

您可以在 PropertyGrid 中提供小图形表示和属性值,类似于为 Image Color 类提供的内容。要在自定义中执行此操作,请从 UITypeEditor 继承,覆盖 GetPaintValueSupported 并返回 true 。然后,覆盖 UITypeEditor.PaintValue 方法,并在自己的方法中使用 PaintValueEventArgs. Graphics 参数绘制图形。最后,将 Editor 特性应用到使用 UITypeEditor 类的类或属性。

下面的屏幕快照显示了结果外观。

图 8:在 PropertyGrid 中显示属性的自定义图形

您也可以提供自己的下拉列表控件,这与 Control.Dock 属性用来为用户提供靠接选择的控件类似。要执行此操作,请从 UITypeEditor 继承,覆盖 GetEditStyle ,然后返回一个 UITypeEditorEditStyle 枚举值,例如 DropDown 。您的自定义下拉列表控件必须从 Control Control 的派生类(例如 UserControl )继承而来。然后,覆盖 UITypeEditor.EditValue 方法。使用 IServiceProvider 参数调用 IServiceProvider.GetService 方法,以获取一个 IWindowsFormsEditorService 实例。最后,调用 IWindowsFormsEditorService.DropDownControl 方法来显示您的自定义下拉列表控件。请记住将 Editor 特性应用到使用 UITypeEditor 类的类或属性中。

下面的屏幕快照显示了结果外观。

图 9:在 PropertyGrid 中显示属性的自定义下拉列表控件

除了使用 TypeEditor UITypeEditor 类外,还可以自定义 PropertyGrid 以显示其他属性选项卡。属性选项卡从 PropertyTab 类继承而来。如果您使用过 Microsoft Visual C#™ .NET 中的属性浏览器,那么就可能看到过自定义的 PropertyTabEvents 选项卡(带有闪电图形的按钮)就是一个自定义的 PropertyTab 。下面的屏幕快照显示了自定义 PropertyTab 的另一个示例。可以使用 PropertyTab 编辑按钮的边界点,以创建自定义的按钮形状。

图 10:在 PropertyGrid 中显示自定义选项卡

如果想在item中增加自定义的显示方式,比如日期选择啦、下拉框啦、甚至文件选择、拾色器等等,我们可以参考如下:

改变 PropertyGrid 控件的编辑风格(1)加入日期控件

编辑日期类型数据

  1. using System;
  2. using System.Windows.Forms;
  3. using System.Drawing.Design;
  4. using System.Windows.Forms.Design;
  5. namespace blog.csdn.net.zhangyuk
  6. {
  7. /// <summary>
  8. /// 在PropertyGrid 上显示日期控件
  9. /// </summary>
  10. public class PropertyGridDateItem : UITypeEditor
  11. {
  12. MonthCalendar dateControl = new MonthCalendar();
  13. public PropertyGridDateItem()
  14. {
  15. dateControl.MaxSelectionCount = 1;
  16. }
  17. public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
  18. {
  19. return UITypeEditorEditStyle.DropDown;
  20. }
  21. public override object EditValue(System.ComponentModel.ITypeDescriptorContext context,
  22. System.IServiceProvider provider, object value)
  23. {
  24. try
  25. {
  26. IWindowsFormsEditorService edSvc =
  27. (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
  28. if (edSvc != null)
  29. {
  30. if (value is string)
  31. {
  32. dateControl.SelectionStart = DateTime.Parse(value as String);
  33. edSvc.DropDownControl(dateControl);
  34. return dateControl.SelectionStart.ToShortDateString();
  35. }
  36. else if (value is DateTime)
  37. {
  38. dateControl.SelectionStart = (DateTime)value;
  39. edSvc.DropDownControl(dateControl);
  40. return dateControl.SelectionStart;
  41. }
  42. }
  43. }
  44. catch (Exception ex)
  45. {
  46. System.Console.WriteLine("PropertyGridDateItem Error : " + ex.Message);
  47. return value;
  48. }
  49. return value;
  50. }
  51. }
  52. }

步骤二:编辑属性类,指定编辑属性。示例如下:

  1. namespace blog.csdn.net.zhangyuk
  2. {
  3. public class SomeProperties
  4. {
  5. private string _finished_time = "";
  6. //……
  7. [Description("完成时间"), Category("属性"), EditorAttribute(typeof(PropertyGridDateItem),
  8. typeof(System.Drawing.Design.UITypeEditor))]
  9. public String 完成时间
  10. {
  11. get { return _finished_date; }
  12. set { _finished_date = value; }
  13. }
  14. //……
  15. }
  16. }

步骤三:设置 PropertyGrid 的属性对象。示例如下:

  1. private void Form1_Load(object sender, System.EventArgs e)
  2. {
  3. this.propertyGrid1.SelectedObject = new SomeProperties();
  4. }

改变 PropertyGrid 控件的编辑风格(2)——编辑多行文本

效果:

适用场合:

1、 编辑多行文本;

2、 编辑长文本。

步骤一:定义从UITypeEditor 派生的类,示例如下:

  1. using System;
  2. using System.Windows.Forms;
  3. using System.Drawing.Design;
  4. using System.Windows.Forms.Design;
  5. namespace blog.csdn.net.zhangyuk
  6. {
  7. /// <summary>
  8. /// PropertyGridMutiText 的摘要说明。
  9. /// </summary>
  10. public class PropertyGridRichText : UITypeEditor
  11. {
  12. public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
  13. {
  14. return UITypeEditorEditStyle.DropDown;
  15. }
  16. public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
  17. {
  18. try
  19. {
  20. IWindowsFormsEditorService edSvc =
  21. (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
  22. if (edSvc != null)
  23. {
  24. if (value is string)
  25. {
  26. RichTextBox box = new RichTextBox();
  27. box.Text = value as string;
  28. edSvc.DropDownControl(box);
  29. return box.Text;
  30. }
  31. }
  32. }
  33. catch (Exception ex)
  34. {
  35. System.Console.WriteLine("PropertyGridRichText Error : " + ex.Message);
  36. return value;
  37. }
  38. return value;
  39. }
  40. }
  41. }

步骤二:编辑属性类,指定编辑属性。示例如下:

  1. namespace blog.csdn.net.zhangyuk
  2. {
  3. public class SomeProperties
  4. {
  5. private string _finished_time = "";
  6. //   ……
  7. // 多行文本编辑框
  8. string _mutiLineSample = "";
  9. [Description("多行文本编辑框"), Category("属性"), EditorAttribute(typeof(PropertyGridRichText),
  10. typeof(System.Drawing.Design.UITypeEditor))]
  11. public String 多行文本
  12. {
  13. get { return _mutiLineSample; }
  14. set { _mutiLineSample = value; }
  15. }
  16. //……
  17. }
  18. }

步骤三:设置PropertyGrid的属性对象。示例如下:

  1. private void Form1_Load(object sender, System.EventArgs e)
  2. {
  3. this.propertyGrid1.SelectedObject = new SomeProperties();
  4. }

改变 PropertyGrid 控件的编辑风格(3)——打开对话框

适用场合:

1、 打开文件、打印设置等通用对话框

2、 打开特定的对话框

步骤一:定义从UITypeEditor 派生的类,以 OpenFileDialog 对话框为例,示例代码如下:

  1. using System;
  2. using System.Windows.Forms;
  3. using System.Drawing.Design;
  4. using System.Windows.Forms.Design;
  5. namespace blog.csdn.net.zhangyuk
  6. {
  7. /// <summary>
  8. /// IMSOpenFileInPropertyGrid 的摘要说明。
  9. /// </summary>
  10. public class PropertyGridFileItem : UITypeEditor
  11. {
  12. public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
  13. {
  14. return UITypeEditorEditStyle.Modal;
  15. }
  16. public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.
  17. IServiceProvider provider, object value)
  18. {
  19. IWindowsFormsEditorService edSvc =
  20. (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
  21. if (edSvc != null)
  22. {
  23. // 可以打开任何特定的对话框
  24. OpenFileDialog dialog = new OpenFileDialog();
  25. dialog.AddExtension = false;
  26. if (dialog.ShowDialog().Equals(DialogResult.OK))
  27. {
  28. return dialog.FileName;
  29. }
  30. }
  31. return value;
  32. }
  33. }
  34. }

步骤二:编辑属性类,指定编辑属性。示例如下:

  1. namespace blog.csdn.net.zhangyuk
  2. {
  3. public class SomeProperties
  4. {
  5. private string _finished_time = "";
  6. //……
  7. // 文件
  8. string _fileName = "";
  9. [Description("文件打开对话框"), Category("属性"), EditorAttribute(typeof(PropertyGridFileItem),
  10. typeof(System.Drawing.Design.UITypeEditor))]
  11. public String 文件
  12. {
  13. get { return _fileName; }
  14. set { _fileName = value; }
  15. }
  16. //……
  17. }
  18. }

步骤三:设置PropertyGrid的属性对象。示例如下:

  1. private void Form1_Load(object sender, System.EventArgs e)
  2. {
  3. this.propertyGrid1.SelectedObject = new SomeProperties();
  4. }

改变 PropertyGrid 控件的编辑风格(4)——加入选择列表

适用场合:限制选择输入

步骤一:定义从UITypeEditor 继承的抽象类:ComboBoxItemTypeConvert。示例如下:

  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. namespace blog.csdn.net.zhangyuk
  5. {
  6. /// IMSTypeConvert 的摘要说明。
  7. public abstract class ComboBoxItemTypeConvert : TypeConverter
  8. {
  9. public Hashtable _hash = null;
  10. public ComboBoxItemTypeConvert()
  11. {
  12. _hash = new Hashtable();
  13. GetConvertHash();
  14. }
  15. public abstract void GetConvertHash();
  16. public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
  17. {
  18. return true;
  19. }
  20. public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
  21. {
  22. int[] ids = new int[_hash.Values.Count];
  23. int i = 0;
  24. foreach (DictionaryEntry myDE in _hash)
  25. {
  26. ids[i++] = (int)(myDE.Key);
  27. }
  28. return new StandardValuesCollection(ids);
  29. }
  30. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  31. {
  32. if (sourceType == typeof(string))
  33. {
  34. return true;
  35. }
  36. return base.CanConvertFrom(context, sourceType);
  37. }
  38. public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object v)
  39. {
  40. if (v is string)
  41. {
  42. foreach (DictionaryEntry myDE in _hash)
  43. {
  44. if (myDE.Value.Equals((v.ToString())))
  45. return myDE.Key;
  46. }
  47. }
  48. return base.ConvertFrom(context, culture, v);
  49. }
  50. public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture,
  51. object v, Type destinationType)
  52. {
  53. if (destinationType == typeof(string))
  54. {
  55. foreach (DictionaryEntry myDE in _hash)
  56. {
  57. if (myDE.Key.Equals(v))
  58. return myDE.Value.ToString();
  59. }
  60. return "";
  61. }
  62. return base.ConvertTo(context, culture, v, destinationType);
  63. }
  64. public override bool GetStandardValuesExclusive(
  65. ITypeDescriptorContext context)
  66. {
  67. return false;
  68. }
  69. }
  70. }

步骤二:定义 ComboBoxItemTypeConvert 的派生类,派生类中实现父类的抽象方法:public abstract void GetConvertHash(); 示例如下:

  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. namespace blog.csdn.net.zhangyuk
  5. {
  6. public class PropertyGridBoolItem : ComboBoxItemTypeConvert
  7. {
  8. public override void GetConvertHash()
  9. {
  10. _hash.Add(0, "是");
  11. _hash.Add(1, "否");
  12. }
  13. }
  14. public class PropertyGridComboBoxItem : ComboBoxItemTypeConvert
  15. {
  16. public override void GetConvertHash()
  17. {
  18. _hash.Add(0, "炒肝");
  19. _hash.Add(1, "豆汁");
  20. _hash.Add(2, "灌肠");
  21. }
  22. }
  23. }

步骤三:编辑属性类,指定编辑属性。示例如下:

  1. namespace blog.csdn.net.zhangyuk
  2. {
  3. public class SomeProperties
  4. {
  5. private string _finished_time = "";
  6. // ……
  7. // 布尔
  8. bool _bool = true;
  9. [Description("布尔"), Category("属性"), TypeConverter(typeof(PropertyGridBoolItem))]
  10. public int 布尔
  11. {
  12. get { return _bool == true ? 0 : 1; }
  13. set { _bool = (value == 0 ? true : false); }
  14. }
  15. // 选择列表
  16. int _comboBoxItems = 0;
  17. [Description("选择列表"), Category("属性"), TypeConverter(typeof(PropertyGridComboBoxItem))]
  18. public int 选择列表
  19. {
  20. get { return _comboBoxItems; }
  21. set { _comboBoxItems = value; }
  22. }
  23. //……
  24. }
  25. }

步骤四:设置PropertyGrid的属性对象。示例如下:

  1. private void Form1_Load(object sender, System.EventArgs e)
  2. {
  3. this.propertyGrid1.SelectedObject = new SomeProperties();
  4. }

如果想改变item的displayname为中文我们该怎么办呢?

运行时自定义PropertyGrid显示属性项目

简述

在PropertyGrid所显示的属性内容包括属性分类(Category)及组件属性,

在一般情况下直接使用PropertyGrid来显示一个对象的所有属性是非常方便的,只需一个语句就能完成:

propertyGrid.SelectedObject = component;

但在实际应用中可能会不需要显示所有属性项目,而是通过外部指定(通过XML等进行描述),这些设置一般情况下在创建组件时用代码中的Attribute来进行具体设置,如所属分类,显示标题等,这只能针对于一些自建的组件可以这么做。

问题描述

像上面所说,在创建自建组件时可以用Attribute的方式来设置PropertyGrid的显示样式,但这种方法不能应用于已有的组件,像系统中的TextBox,Button等,除非自己建立一个由这些组件派生的类,当然这样做会加大复杂度。像要实现下面所显示的这种效果在实际操作时会很麻烦。

左图是TextBox原有的所有属性,右图是经过处理后的属性

解决方法

在.Net中提供了一个自定义类型说明的接口(System.ComponentModel.ICustomTypeDescriptor),PropertyGrid可以直接自动处理用此接口生成的对象,因此在处理这个问题的时候只需要创建一个基于这个接口的处理类就可以达到世期望的目标,在这个接口中提供了GetProperties方法用于返回所选组件的所有属性,因此我们可以通过这个方法可以对我们所需要的属性进行过滤,下面是一段GetPropertys的处理代码:

  1. public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
  2. {
  3. List<CustomPropertyDescriptor> tmpPDCLst = new List<CustomPropertyDescriptor>();
  4. PropertyDescriptorCollection tmpPDC = TypeDescriptor.GetProperties(mCurrentSelectObject, attributes);
  5. IEnumerator tmpIe = tmpPDC.GetEnumerator();
  6. CustomPropertyDescriptor tmpCPD;
  7. PropertyDescriptor tmpPD;
  8. while (tmpIe.MoveNext())
  9. {
  10. tmpPD = tmpIe.Current as PropertyDescriptor;
  11. if (mObjectAttribs.ContainsKey(tmpPD.Name))
  12. {
  13. tmpCPD = new CustomPropertyDescriptor(mCurrentSelectObject, tmpPD);
  14. tmpCPD.SetDisplayName(mObjectAttribs[tmpPD.Name]);
  15. //此处用于处理属性分类的名称,可以在XML等设置文件中进行设置,在这段代码中只是简单的在分类后加了“中文”两个字
  16. tmpCPD.SetCategory(tmpPD.Category + "中文");
  17. tmpPDCLst.Add(tmpCPD);
  18. }
  19. }
  20. return new PropertyDescriptorCollection(tmpPDCLst.ToArray());
  21. }

当然在进行属性过虑之后,PropertyGrid中所显示的属性名称都还是原有名称,若想同时改变在PropertyGrid中显示出来的名称则需要重写PropertyDescriptor中的部分方法,在上面这段代码中的CustomPropertyDescriptor就是一个基于PropertyDescriptor的类。

在CustomPropertyDescriptor类中最主要的是重写DisplayName与Category这两个属性,但由于在PropertyDescriptor中这两个属性是只读的,因此在这个类中需要加入两个用于设置这两个属性的方法(或直接用Field)在这里我使用了SetDispalyName与SetCategory这两个方法:

  1. private string mCategory;
  2. public override string Category
  3. {
  4. get { return mCategory; }
  5. }
  6. private string mDisplayName ;
  7. public override string DisplayName
  8. {
  9. get { return mDisplayName; }
  10. }
  11. public void SetDisplayName(string pDispalyName)
  12. {
  13. mDisplayName = pDispalyName;
  14. }
  15. public void SetCategory(string pCategory)
  16. {
  17. mCategory = pCategory;
  18. }

就这样的几步,便可以将PropertyGrid中显示的内容完全自定义。

在写ICustomTypeDescriptor接口时,其他的一些方法可以用TypeDescriptor直接返回相关方法调用,并在GetPropertyOwner方法中应返回当前选择对象否则将不会对修改值起任何作用

  1. public object GetPropertyOwner(PropertyDescriptor pd)
  2. {
  3. return mCurrentSelectObject;
  4. }

在写CustomPropertyDescriptor类时需要一个PropertyDescriptor对象,在实现一些方法时直接返回这个对象的值。

当然也可以通过这个方法来自定义一些Events的输出,

使用方法

  1. //加载组件属性,从XML文件载入,此处为Button
  2. XmlNode tmpXNode = mXDoc.SelectSingleNode("Components/Component[@Name=/"Button/"]");
  3. //选择属性设置
  4. XmlNodeList tmpXPropLst = tmpXNode.SelectNodes("Propertys/Property");
  5. //创建CustomProperty对象
  6. CustomProperty cp = new CustomProperty(sender, tmpXPropLst);
  7. //设置PropertyGrid选择对象
  8. propertyGrid1.SelectedObject = cp;

对于显示的item顺序如果想自定义怎么办呢?

  1. //
  2. // 摘要:
  3. //     使用该集合的默认排序(通常为字母顺序)对集合中的成员进行排序。
  4. public virtual PropertyDescriptorCollection Sort();
  5. //
  6. // 摘要:
  7. //     使用指定的 System.Collections.IComparer 对此集合中的成员排序。
  8. public virtual PropertyDescriptorCollection Sort(IComparer comparer);
  9. //
  10. // 摘要:
  11. //     对此集合中的成员排序。首先应用指定的顺序,然后应用此集合的默认排序,后者通常为字母顺序。
  12. public virtual PropertyDescriptorCollection Sort(string[] names);
  13. //
  14. // 摘要:
  15. //     对此集合中的成员排序。首先应用指定的顺序,然后使用指定的 System.Collections.IComparer 进行排序。
  16. public virtual PropertyDescriptorCollection Sort(string[] names, IComparer comparer);
  17. /// <summary>
  18. /// 返回此类型说明符所表示的对象的已筛选属性描述符的集合。
  19. /// </summary>
  20. /// <param name="attributes">用作筛选器的属性数组。它可以是 null。</param>
  21. /// <returns>
  22. /// 一
    个 <see cref="T:System.ComponentModel.PropertyDescriptorCollection"&
    gt;</see>,包含此类型说明符所表示的对象的属性说明。默认
    为 <see cref="F:System.ComponentModel.PropertyDescriptorCollection.Empty"&
    gt;</see>。
  23. /// </returns>
  24. public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
  25. {
  26. int i = 0;
  27. //parameterList是外面传入的待显示数据。
  28. PropertyDescriptor[] newProps = new PropertyDescriptor[parameterList.Count];
  29. foreach (ConfigParameter parameter in parameterList)
  30. {
  31. //由ConfigParameter,你自己定义的类型,转成PropertyDescriptor类型:
  32. newProps[i++] = new SystemOptionsPropertyDescriptor(parameter, true, attributes);
  33. }
  34. //取得了PropertyDescriptor[] newProps;现在可以对它排序,否则就按照newProps的原有顺序显示;
  35. //1.直接返回
  36. PropertyDescriptorCollection tmpPDC = new PropertyDescriptorCollection(newProps);
  37. return tmpPDC;
  38. //2.默认字母顺序
  39. PropertyDescriptorCollection tmpPDC = new PropertyDescriptorCollection(newProps);
  40. return tmpPDC.Sort();
  41. //3.数组的顺序:sortName就是属性的顺序,内容就是各个属性名。
  42. string[] sortName = new string[] { "a","b","c","d" };
  43. return tmpPDC.Sort(sortName);
  44. //4.comparer规则定义的排序方式
  45. ParameterListComparer myComp = new ParameterListComparer();//ParameterListComparer : IComparer
  46. return tmpPDC.Sort(myComp);
  47. //5.先数组,后comparer
  48. return tmpPDC.Sort(sortName,myComp);
  49. //而我采用的是把数据传入之前就排序完毕的方法:即调用之前,把数据parameterList排好顺序,再 1.直接返回。
  50. }
  51. //对于数组排序方法,可以声明称一个事件,由外部传入属性的实现顺序:
  52. public delegate string[] SortEventHandler();
  53. public class CustomTypeDescriptorExtend : CustomTypeDescriptor
  54. {
  55. public SortEventHandler OnSort;
  56. //......其它代码
  57. public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
  58. {
  59. string[] sortName = OnSort();
  60. PropertyDescriptorCollection tmpPDC = TypeDescriptor.GetProperties(typeof(CustomTypeDescriptorExtend), attributes);
  61. return tmpPDC.Sort(sortName);
  62. }
  63. }
  64. //使用时:
  65. public MyMethod()
  66. {
  67. CustomTypeDescriptorExtend s = new CustomTypeDescriptorExtend();
  68. s.OnSort += new SortEventHandler(SetSortors);
  69. PropertyGrid1.SelectedObject = s;
  70. PropertyGrid1.PropertySort = PropertySort.Categorized;
  71. }
  72. public string[] SetSortors()
  73. {
  74. return new string[] { "B", "A", "C" };
  75. }

如果希望在程序运行中修改某些属性值的话,应该怎么办呢?

  1. void SetPropertyVisibility(object obj, string propertyName, bool visible)
  2. {
  3. Type type = typeof(BrowsableAttribute);
  4. PropertyDescriptorCollection props = TypeDescriptor.GetProperties(obj);
  5. AttributeCollection attrs = props[propertyName].Attributes;
  6. FieldInfo fld = type.GetField("browsable", BindingFlags.Instance | BindingFlags.NonPublic);
  7. fld.SetValue(attrs[type], visible);
  8. }
  9. void SetPropertyReadOnly(object obj, string propertyName, bool readOnly)
  10. {
  11. Type type = typeof(System.ComponentModel.ReadOnlyAttribute);
  12. PropertyDescriptorCollection props = TypeDescriptor.GetProperties(obj);
  13. AttributeCollection attrs = props[propertyName].Attributes;
  14. FieldInfo fld = type.GetField("isReadOnly", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.CreateInstance);
  15. fld.SetValue(attrs[type], readOnly);
  16. }

总之,PropertyGrid通过很多技巧能够达到很炫的效果,不过由于PropertyGrid与对象严格绑定,需要大量的
convert/uiedittype/attributes等配合实现,同时要注意在使用过程中PropertyGrid的改变就代表了内存对象的改
变,这一点一定要记住。同时字段的属性,特别是类型属性一定要严格控制好,否则convert过程中就会异常了。

本文转自http://blog.csdn.net/luyifeiniu/article/details/5426960

C# PropertyGrid控件应用心得的更多相关文章

  1. C&num; PropertyGrid控件应用心得 【转】

    源文 : http://blog.csdn.net/luyifeiniu/article/details/5426960 c#stringattributesobjectmicrosoftclass ...

  2. PropertyGrid控件由浅入深&lpar;二&rpar;:基础用法

    目录 PropertyGrid控件由浅入深(一):文章大纲 PropertyGrid控件由浅入深(二):基础用法 控件的外观构成 控件的外观构成如下图所示: PropertyGrid控件包含以下几个要 ...

  3. C&num; 如何定义让PropertyGrid控件显示&lbrack;&period;&period;&period;&rsqb;按钮,并且点击后以下拉框形式显示自定义控件编辑属性值

    关于PropertyGrid控件的详细用法请参考文献: 1.C# PropertyGrid控件应用心得 2.C#自定义PropertyGrid属性 首先定义一个要在下拉框显示的控件: using Sy ...

  4. PropertyGrid控件由浅入深&lpar;一&rpar;:文章大纲

    Winform中PropertyGrid控件是一个非常好用的对象属性编辑工具,对于Key-Value形式的数据的处理也是非常的好用. 因为Property控件设计良好,在很小的空间内可以展示很多的内容 ...

  5. WinForm小白的WPF初试一:从PropertyGrid控件,输出内容到Word(上)

    学WinForm也就半年,然后转到WPF,还在熟悉中.最近拿到一个任务:从PropertyGrid控件,输出内容到Word.难点有: 一.PropertyGrid控件是WinForm控件,在WPF中并 ...

  6. WinForm窗体PropertyGrid控件的使用

    使用过 Microsoft Visual Basic 或 Microsoft Visual Studio .NET的朋友,一定使用过属性浏览器来浏览.查看或编辑一个或多个对象的属性..NET 框架 P ...

  7. propertyGrid控件 z

    1.如果属性是enum类型,那么自然就是下拉的. 2.如果是你自定义的下拉数据,那么需要用到转换属性标签TypeConverter 参见: http://blog.csdn.net/luyifeini ...

  8. 自定义PropertyGrid控件【转】

    自定义PropertyGrid控件 这篇随笔其实是从别人博客上载录的.感觉很有价值,整理了一下放在了我自己的博客上,希望原作者不要介意. 可自定义PropertyGrid控件的属性.也可将属性名称显示 ...

  9. 关于PropertyGrid控件的排序问题

    前些天,由于在项目中需要用到PropertyGrid这个控件,展现其所在控件的某些属性,由于有些控件的属性较多,不易浏览,而且PropertyGrid的排序默认的按照字母的顺序排列的,这样导致在在某些 ...

随机推荐

  1. POJ 2010 - Moo University - Financial Aid 初探数据结构 二叉堆

    考虑到数据结构短板严重,从计算几何换换口味= = 二叉堆 简介 堆总保持每个节点小于(大于)父亲节点.这样的堆被称作大根堆(小根堆). 顾名思义,大根堆的数根是堆内的最大元素. 堆的意义在于能快速O( ...

  2. Loadrunner中web&lowbar;reg&lowbar;save&lowbar;param的使用详解

    [摘要]利用实际案例说明如何使用Mercury LoadRunner提取包含在HTML页内的动态信息并创建参数. [关键词]性能测试,压力测试,Mercury LoadRunner 应用范围 在使用L ...

  3. PL&sol;SQL程序中调用Java代码(转)

    主要是学习PL/SQL调用JAVA的方法. 平台:WINDOWS 1.首先使用IDE写好需要调用的java代码,再添加"create or replace and compile java ...

  4. AngularJS 1&period;x系列:AngularJS服务-Service、Factory、Provider、Value及Constant(5)

    1. AngularJS服务 AngularJS可注入类型包括:Service.Factory.Provider.Value及Constant. 2. Service AngularJS Servic ...

  5. jdk各个版本之间的差异

    背景:求职过程中,这个问题反复被问到.如果答不上来,只能说明基本功不扎实,并不能说自己擅长java. 技术趣味史-Java 各个版本的特性 Java 5 2004 年 Sun 公司发布 J2SE5(没 ...

  6. jquery特殊字符转义方法

    //特殊字符转义function escapeJquery(srcString) { // 转义之后的结果 var escapseResult = srcString; // javascript正则 ...

  7. Exception from System&period;loadLibrary&lpar;smjavaagentapi&rpar; java&period;lang&period;UnsatisfiedLinkError&colon; no smjavaagentapi in java&period;library&period;path

    可能原因: 缺少smjavaagentapi.jar文件或者libsjavaagentapi.so缺少相关的依赖包. 解决方法: 1. 检查sso的lib下面是否有smjavaagentapi.jar ...

  8. nable to execute dex&colon; Multiple dex files define Lcom&sol;chinaCEB&sol;cebActivity&sol;R

    用proguaid 只混淆Android项目的src下的包的话,如果出现了上面的问题: nable to execute dex: Multiple dex files define Lcom/chi ...

  9. python2含有中文路径报错解决办法&lbrack;&bsol;xe4&bsol;xbf&bsol;xa1&bsol;xe6&bsol;x81&bsol;xaf&rsqb;

    如图所示 百度的解决办法大多数是针对python3版本的,在脚本开头加# -*- coding:utf-8 -*-,但是python2版本加了编码格式,还是报错,具体解决办法是:path =unico ...

  10. vue 根据下拉框动态切换form的rule

    taskCategorySelect (val) { // 任务类别下拉选择 if ( val == 5 ) { this.cameraORgateway = false; // true不可以使用 ...