Visual StudioのPropertyGrid:オブジェクトコレクションに関連付けられたフィールドの表示

PropertyGridを使用すると、さまざまなクラス構造を簡単に編集できる形式で表示できます。そのためには、クラスのオブジェクトをそれに関連付ければ十分です。 ただし、追加のコードを記述せずにすべての構成をすぐに表示できるわけではありません。 この記事では、コンテキストでPropertyGridを使用した私の経験についてお話したいと思います。

1.オブジェクトのコレクションであるエントリのドロップダウンリストを表示します。



画像



2.コレクションエディタを呼び出すオブジェクトのコレクションに関連付けられたフィールドを表示します。



画像



例として、従業員情報管理を実装するタスクを取りましたが、これはEmployeeクラスによって実装され、クラスには2つのプロパティが含まれています。 最初はタイプJobTitleのプロパティ(ドロップダウンリストの従業員の位置)、2番目はタイプJobTitleCollectionのプロパティ(コレクションエディターの従業員投稿のコレクション)です。 タスクは、記事の冒頭の2つの図に示すように、PropertyGridにEmployeeクラスのオブジェクトを表示することです。 ComboBox要素のように、ドロップダウンリストを使用して、ユーザーが従業員の投稿の1つを選択できるフィールドを表示します。 投稿の関連付けられたコレクションのエディターを呼び出すことができるフィールドを表示し、コレクションの変更が完了したら、PropertyGridに投稿をリストとして表示します。

さらに詳しく考えてみましょう。



従業員クラス

  1. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  2. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  3. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  4. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  5. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  6. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  7. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  8. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  9. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  10. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  11. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  12. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  13. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  14. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  15. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  16. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  17. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  18. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  19. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  20. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  21. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  22. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  23. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  24. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  25. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  26. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  27. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  28. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  29. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  30. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



  31. /// <summary> /// /// </summary> [DisplayName( "" ), Description( " " ), Category( "" )] public class Employee { private JobTitle jt = new JobTitle (); /// <summary> /// , /// ( JobTitle) /// </summary> [DisplayName( "" ), Description( " " ), Category( " " )] [TypeConverter( typeof ( JobTitleTypeConverter ))] public JobTitle jobtitle { get { return jt; } set { jt = value ; } } private JobTitleCollection jobTitles = new JobTitleCollection (); /// <summary> /// , /// PropertyGrid /// </summary> [Description( " " ), Category( "" ), DisplayName( "" )] [TypeConverter( typeof ( JobTitleCollectionConverter ))] public JobTitleCollection JobTitles { get { return jobTitles; } set { jobTitles = value ; } } }



JobTitleジョブクラス
  1. /// <summary>
  2. ///従業員の位置
  3. /// </ summary>
  4. [DisplayName( "Position" )、Description( "Position of the employee" )、Category( "Employeeに関する情報" )]
  5. [TypeConverter( typeofJobTitleConverter ))]
  6. パブリッククラス JobTitleMyItem
  7. {
  8. /// <summary>
  9. ///コンストラクター
  10. /// </ summary>
  11. 公開 ジョブタイトル (){}
  12. /// <summary>
  13. ///コンストラクター
  14. /// </ summary>
  15. /// <param name = "ID">レコードID </ param>
  16. /// <param name = "ItemName">名前</ param>
  17. public JobTitleint ID、 文字列 ItemName)
  18. {
  19. this .ID = ID;
  20. this .ItemName = ItemName;
  21. }
  22. }
問題解決

PropertyGridオブジェクトが投稿のドロップダウンリストを正しく表示できるように、JobTitle型を変換するクラスを実装する必要があります。 Employeeクラスのオブジェクトの作成時に、リストに表示するためにいくつかの投稿がコレクションに追加され、その後、コレクションをドロップダウンリストに表示し、EmployeeクラスのオブジェクトのJobTitleCollectionフィールドで編集できると想定されます。 -JobTitleTypeConverter。 さらに、このクラスに加えて、問題の解決に必要な追加のクラスが実装されます。

こののソースコードは利用可能です。 リンクは記事の最後に複製されます。

PropertyGridに表示されるオブジェクト情報の管理は、属性を使用して実行されることにすぐに注意します。 これにより、目的の情報表示スタイルを設定できます。 さらに、属性はソリューションの実装で積極的に使用されます。

  1. /// <summary>
  2. /// JobTitle型変換クラス
  3. ///文字列に変換してJobTitleに戻す
  4. ///また、投稿のコレクションをドロップダウンリストの形式で表示できます
  5. /// </ summary>
  6. パブリッククラス JobTitleTypeConverterExpandableObjectConverter
  7. {
  8. /// <summary>
  9. ///オブジェクトが標準の値セットをサポートするかどうかを示す値を返します。
  10. ///リストから選択できます。
  11. ///値を強制的にtrueにしないと、チャンスがあります
  12. ///ハンドラーがGetStandardValuesメソッドを受け入れず、表示されないこと
  13. ///選択する値のリスト
  14. /// </ summary>
  15. /// <param name = "context"> </ param>
  16. /// <returns> </ returns>
  17. public override bool GetStandardValuesSupported( ITypeDescriptorContext context)
  18. {
  19. trueを返します
  20. }
  21. /// <summary>
  22. ///このメソッドは除外できますが、将来このコンバータークラスを使用できるようになります
  23. ///異なるクラスから呼び出すため。
  24. ///これを行うには、プロパティを含む別のクラスの名前をケースセクションに追加するだけです
  25. ///ドロップダウンリンク機能を備えたJobTitle
  26. /// </ summary>
  27. /// <param name = "context"> </ param>
  28. /// <returns> </ returns>
  29. プライベート JobTitleCollection GetCollection(System.ComponentModel。ITypeDescriptorContextコンテキスト)
  30. {
  31. JobTitleCollectionコレクション= 新しい JobTitleCollection ();
  32. スイッチ (context.Instance.GetType()。名前)
  33. {
  34. ケース 「従業員」
  35. collection =((Employee)context.Instance).JobTitles;
  36. 休憩 ;
  37. デフォルト
  38. collection =((Employee)context.Instance).JobTitles;
  39. 休憩 ;
  40. }
  41. 返品コレクション;
  42. }
  43. /// <summary>
  44. ///メソッドは、ドロップダウンに表示する投稿のコレクションから値のリストを返します
  45. ///従業員の位置の値のリストPropertyGrid
  46. /// </ summary>
  47. /// <param name = "context"> </ param>
  48. /// <returns> </ returns>
  49. TypeConverterをオーバーライドしますStandardValuesCollection GetStandardValues( ITypeDescriptorContextコンテキスト)
  50. {
  51. 新しい StandardValuesCollectionを 返す (GetCollection(コンテキスト));
  52. }
  53. /// <summary>
  54. ///メソッドは、ユーザーから取得したプロパティ値を変換できるかどうかを確認します
  55. ///必要な型に
  56. /// </ summary>
  57. /// <param name = "context"> </ param>
  58. /// <param name = "destinationType"> </ param>
  59. /// <returns> </ returns>
  60. パブリックオーバーライドbool CanConvertTo( ITypeDescriptorContextコンテキスト、 type destinationType)
  61. {
  62. if (destinationType.Equals( typeofstring )))
  63. {
  64. trueを返します
  65. }
  66. 他に
  67. {
  68. falseを返します
  69. }
  70. }
  71. /// <summary>
  72. /// JobTitle型を文字列に変換して、PropertyGridに値を表示します
  73. /// </ summary>
  74. /// <param name = "context"> </ param>
  75. /// <param name = "culture"> </ param>
  76. /// <param name = "value"> </ param>
  77. /// <param name = "destinationType"> </ param>
  78. /// <returns> </ returns>
  79. パブリックオーバーライドオブジェクト ConvertTo( ITypeDescriptorContextコンテキスト、
  80. System.Globalization。 CultureInfoカルチャ、 オブジェクト値Type destinationType)
  81. {
  82. if (destinationType == typeofstring )&& value is JobTitle
  83. {
  84. JobTitleアイテム=( JobTitle ;
  85. return item.FullName;
  86. }
  87. return base .ConvertTo(context、culture、 value 、destinationType);
  88. }
  89. /// <summary>
  90. ///文字列表現のJobTitle型への逆変換の可能性を確認します
  91. /// </ summary>
  92. /// <param name = "context"> </ param>
  93. /// <param name = "sourceType"> </ param>
  94. /// <returns> </ returns>
  95. パブリックオーバーライドbool CanConvertFrom( ITypeDescriptorContextコンテキスト、 Type sourceType)
  96. {
  97. if (sourceType.Equals( typeofstring )))
  98. {
  99. trueを返します
  100. }
  101. 他に
  102. {
  103. falseを返します
  104. }
  105. }
  106. /// <summary>
  107. ///投稿の文字列表現をJobTitleタイプに変換します。
  108. ///この変換の欠点は、形式に応じて型変換が正しく機能することです
  109. /// JobTitle型の文字列表現、つまり重複値から。
  110. /// </ summary>
  111. /// <param name = "context"> </ param>
  112. /// <param name = "culture"> </ param>
  113. /// <param name = "value"> </ param>
  114. /// <returns> </ returns>
  115. パブリックオーバーライドオブジェクト ConvertFrom( ITypeDescriptorContextコンテキスト、
  116. System.Globalization。 CultureInfoカルチャ、 オブジェクト値
  117. {
  118. ifvalue .GetType()== typeofstring ))
  119. {
  120. JobTitle itemSelected = GetCollection(context).Count.Equals( 0 )?
  121. new JobTitle ():GetCollection(context)[ 0 ];
  122. foreachGetCollection (コンテキスト)のJobTitleアイテム)
  123. {
  124. string sCraftName = Item.FullName;
  125. if (sCraftName.Equals(( stringvalue ))
  126. {
  127. itemSelected =アイテム;
  128. }
  129. }
  130. return itemSelected;
  131. }
  132. 他に
  133. return base .ConvertFrom(context、culture、 value );
  134. }
  135. /// <summary>
  136. ///標準値のコレクションが完全なリストであるかどうかを示す値を返します。
  137. ///メソッドによって返されます。
  138. ///
  139. /// false-データは手動で入力できます
  140. /// true-リストからの選択のみ
  141. /// </ summary>
  142. /// <param name = "context"> </ param>
  143. /// <returns> </ returns>
  144. public override bool GetStandardValuesExclusive( ITypeDescriptorContext context)
  145. {
  146. trueを返します
  147. }
  148. /// <summary>
  149. ///メソッドコンストラクター
  150. /// </ summary>
  151. public JobTitleTypeConverter (){}
  152. }
ポストコレクションクラス

コレクションクラスを実装するには、CollectionBaseクラスからそれを継承し、ICustomTypeDescriptorインターフェイスを実装する必要があります。 これにより、コレクションを使用してPropertyGridに正しく表示できます。 これを行うには、JobTitleCollectionPropertyDescriptorコレクションのプロパティ記述子クラスの実装を使用します。
  1. /// <summary>
  2. ///コレクションの投稿クラス
  3. ///コレクションプロパティの内容を説明する情報を提供するメソッドが含まれています
  4. ///およびPropertyGridに表示
  5. /// </ summary>
  6. [DisplayName( "投稿リスト" )、説明( "従業員の投稿リスト" )、カテゴリ( "従業員情報" )]
  7. パブリッククラス JobTitleCollectionCollectionBaseICustomTypeDescriptor
  8. {
  9. #regionコレクションメソッド
  10. public void Add( JobTitle Item)
  11. {
  12. this .List.Add(アイテム);
  13. }
  14. public void remove( ジョブタイトル項目)
  15. {
  16. this .List.Remove(アイテム);
  17. }
  18. public JobTitle this [ int Index]
  19. {
  20. get { returnJobTitlethis .List [インデックス]; }
  21. }
  22. #endregion
  23. #region ICustomTypeDescriptorインターフェースの実装
  24. public String GetClassName()
  25. {
  26. Return TypeDescriptor.GetClassName( thistrue );
  27. }
  28. public AttributeCollection GetAttributes()
  29. {
  30. Return TypeDescriptor.GetAttributes( thistrue );
  31. }
  32. public String GetComponentName()
  33. {
  34. Return TypeDescriptor.GetComponentName( thistrue );
  35. }
  36. パブリック TypeConverter GetConverter()
  37. {
  38. Return TypeDescriptor.GetConverter( thistrue );
  39. }
  40. public EventDescriptor GetDefaultEvent()
  41. {
  42. Return TypeDescriptor.GetDefaultEvent( thistrue );
  43. }
  44. public PropertyDescriptor GetDefaultProperty()
  45. {
  46. Return TypeDescriptor.GetDefaultProperty( thistrue );
  47. }
  48. パブリックオブジェクト GetEditor( タイプ editorBaseType)
  49. {
  50. TypeDescriptor.GetEditor( this 、editorBaseType、 true );を返します
  51. }
  52. public EventDescriptorCollection GetEvents( 属性 []属性)
  53. {
  54. TypeDescriptor.GetEvents( this 、attributes、 true );を返します。
  55. }
  56. public EventDescriptorCollection GetEvents()
  57. {
  58. return TypeDescriptor.GetEvents( thistrue );
  59. }
  60. パブリックオブジェクト GetPropertyOwner( PropertyDescriptor pd)
  61. {
  62. これを返す ;
  63. }
  64. public PropertyDescriptorCollection GetProperties( 属性 []属性)
  65. {
  66. GetProperties()を返します。
  67. }
  68. public PropertyDescriptorCollection GetProperties()
  69. {
  70. PropertyDescriptorCollection pds = new PropertyDescriptorCollectionnull );
  71. forint i = 0 ; i < this .List.Count; i ++)
  72. {
  73. JobTitleCollectionPropertyDescriptor pd = new JobTitleCollectionPropertyDescriptorthis 、i);
  74. pds.Add(pd);
  75. }
  76. pdsを返します。
  77. }
  78. #endregion
  79. }
追加のJobTitleオブジェクトコレクションプロパティの説明クラス
  1. /// <summary>
  2. /// JobTitleオブジェクトコレクションプロパティの説明クラス
  3. /// </ summary>
  4. パブリッククラス JobTitleCollectionPropertyDescriptorPropertyDescriptor
  5. {
  6. プライベート JobTitleCollectionコレクション= null ;
  7. private int index = -1 ;
  8. public JobTitleCollectionPropertyDescriptor (JobTitleCollection coll、 int idx):
  9. ベース"#" + idx.ToString()、 null
  10. {
  11. this .collection = coll;
  12. this .index = idx;
  13. }
  14. AttributeCollection属性をオーバーライドします
  15. {
  16. 得る
  17. {
  18. 新しい AttributeCollectionnull );
  19. }
  20. }
  21. パブリックオーバーライドbool CanResetValue( オブジェクトコンポーネント)
  22. {
  23. trueを返します
  24. }
  25. パブリックオーバーライド タイプ ComponentType
  26. {
  27. 得る
  28. {
  29. この .collection.GetType()を返します。
  30. }
  31. }
  32. パブリックオーバーライド文字列 DisplayName
  33. {
  34. 得る
  35. {
  36. JobTitle Item = this .collection [インデックス];
  37. return Item.FullName;
  38. }
  39. }
  40. 公開オーバーライド文字列説明
  41. {
  42. 得る
  43. {
  44. JobTitle Item = this .collection [インデックス];
  45. StringBuilder sb = new StringBuilder ();
  46. sb.Append(Item.ItemName);
  47. sb.Append( "、" );
  48. sb.Append(インデックス+ 1 );
  49. return sb.ToString();
  50. }
  51. }
  52. パブリックオーバーライドオブジェクト GetValue( オブジェクトコンポーネント)
  53. {
  54. この .collection [インデックス]を返します。
  55. }
  56. パブリックオーバーライドbool IsReadOnly
  57. {
  58. get { falseを返す ; }
  59. }
  60. パブリックオーバーライド文字列 Name
  61. {
  62. get { return "#" + index.ToString(); }
  63. }
  64. パブリックオーバーライド タイプ PropertyType
  65. {
  66. get { this .collection [index] .GetType();を返します。 }
  67. }
  68. public override void ResetValue( オブジェクトコンポーネント)
  69. {
  70. }
  71. public override bool ShouldSerializeValue( オブジェクトコンポーネント)
  72. {
  73. trueを返します
  74. }
  75. パブリックオーバーライドvoid SetValue( オブジェクトコンポーネント、 オブジェクト値
  76. {
  77. }
  78. }
ポストコレクションコンバーターの追加クラス
  1. /// <summary>
  2. /// PropertyGridのコレクションの標準表示のコンバーターのクラス
  3. ///指定「(Collection)」を「(Posts ...)」に置き換えます
  4. /// </ summary>
  5. パブリッククラス JobTitleCollectionConverterExpandableObjectConverter
  6. {
  7. パブリックオーバーライドオブジェクト ConvertTo( ITypeDescriptorContextコンテキスト、System.Globalization。CultureInfoカルチャ、
  8. オブジェクト値タイプ destType)
  9. {
  10. if (destType == typeofstring )&& value is JobTitleCollection)
  11. {
  12. return "(Posts ...)" ;
  13. }
  14. return base .ConvertTo(context、culture、 value 、destType);
  15. }
  16. }
ポストタイプコンバーターの追加クラス
  1. /// <summary>
  2. ///コレクションエディターで正しく表示するためにJobTitle型変換を実行するクラス
  3. ///実装されていない場合、JobTitleコレクションのエディターウィンドウに内部名が表示されます
  4. /// JobTitleクラス
  5. /// </ summary>
  6. パブリッククラス JobTitleConverterExpandableObjectConverter
  7. {
  8. パブリックオーバーライドオブジェクト ConvertTo( ITypeDescriptorContextコンテキスト、System.Globalization。CultureInfoカルチャ、
  9. オブジェクト値タイプ destType)
  10. {
  11. if (destType == typeofstring )&& value is JobTitle)
  12. {
  13. JobTitle jobtitle =(JobTitle) value ;
  14. return jobtitle.FullName;
  15. }
  16. return base .ConvertTo(context、culture、 value 、destType);
  17. }
  18. }
従業員のクラスと役職のクラスの祖先
  1. /// <summary>
  2. ///エンティティの祖先クラス
  3. /// </ summary>
  4. パブリッククラス MyItem
  5. {
  6. private int iID = -1 ;
  7. /// <summary>
  8. ///レコードID
  9. /// </ summary>
  10. [説明( "アイテム番号" )、カテゴリ( "コレクション" )、DisplayName( "番号" )]
  11. パブリックint ID
  12. {
  13. get { return iID; }
  14. {iID = value ; }
  15. }
  16. プライベート文字列 sItemName = "" ;
  17. /// <summary>
  18. ///名前
  19. /// </ summary>
  20. [説明( "アイテム名" )、カテゴリ( "コレクション" )、DisplayName( "名前" )]
  21. パブリックストリング ItemName
  22. {
  23. get { return sItemName; }
  24. セット {sItemName = value ; }
  25. }
  26. /// <summary>
  27. ///オブジェクトのフルネーム
  28. /// </ summary>
  29. [閲覧可能( false )]
  30. パブリックストリング FullName
  31. {
  32. 得る
  33. {
  34. return String .Format( "{0} No. {1}" 、ItemName、ID.ToString());
  35. }
  36. }
  37. }
サンプルのソースコードをダウンロードできます。

この記事はPropertyGrid FAQに基づいて作成され、それを補完することを目的としています。



All Articles