0

既存のウィンドウフォームアプリケーションがあります。アプリケーションにはプロパティグリッドがあります。プロパティの値は、実行時にユーザーが設定します。私がやりたいのは、コードから特定のプロパティの現在の値を決定することです。私は部分的に成功しました。カテゴリと物件名情報を取得できます。ユーザーが設定したプロパティの現在の値と、他の2つの関連する質問を取得するのに問題があります。

私が使用しているコードは次のとおりです。

 // ICustomTypeDescriptor Interface Implementation
 public AttributeCollection GetAttributes()
 {
      return TypeDescriptor.GetAttributes(GetType());
 }

 public string GetClassName()
 {
      return TypeDescriptor.GetClassName(GetType());
 }

 public string GetComponentName()
 {
      return TypeDescriptor.GetComponentName(GetType());
 }

 public TypeConverter GetConverter()
 {
      return TypeDescriptor.GetConverter(GetType());
 }

 public EventDescriptor GetDefaultEvent()
 {
      return TypeDescriptor.GetDefaultEvent(GetType());
 }

 public PropertyDescriptor GetDefaultProperty()
 {
      return TypeDescriptor.GetDefaultProperty(GetType());
 }

 public object GetEditor(Type editorBaseType)
 {
      return TypeDescriptor.GetEditor(GetType(), editorBaseType);
 }

 public EventDescriptorCollection GetEvents(Attribute[] attributes)
 {
      return TypeDescriptor.GetEvents(GetType(), attributes);
 }

 public EventDescriptorCollection GetEvents()
 {
      return TypeDescriptor.GetEvents(GetType());
 }

 public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
 {
      // ... This returns a list of properties.
      PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(GetType(), attributes);
      PropertyDescriptor[] arr = new PropertyDescriptor[pdc.Count];
      pdc.CopyTo(arr, 0);
      PropertyDescriptorCollection propertyCollection = new PropertyDescriptorCollection(arr);

      ModifyProperties(propertyCollection); // modifies which properties are visible

      // temporary code to get the program to print out the properties
foreach (PropertyDescriptor pd in propertyCollection)
{
           Print("input category = "+pd.Category);
           Print("input display name = "+pd.DisplayName);
           Print("input name = "+pd.Name);
        // Print("input value = "+pd.GetValue(Input).ToString()); <--- Does NOT work
}

return propertyCollection;
 }

 public PropertyDescriptorCollection GetProperties()
 {
      return TypeDescriptor.GetProperties(GetType());
 }

 public object GetPropertyOwner(PropertyDescriptor pd)
 {
      return this;
 }

私の質問は次のとおりです。

  1. プロパティの値を取得するにはどうすればよいですか?たとえば、プロパティのアスペクト比があります。その表示名はアスペクト比であり、その名前はアスペクト比です。その値は5です。実行時にコードを介してこれを取得するにはどうすればよいですか?

  2. プロパティを注文するにはどうすればよいですか。上記のアプローチを使用してプロパティを注文しようとしましたが、注文に失敗しました。どうすればいいのかわかりません。

任意の提案をいただければ幸いです。ありがとうございました。

4

1 に答える 1

1

多分これ

foreach (PropertyDescriptor pd in propertyCollection)
{
     Print("input category = "+pd.Category);
     Print("input display name = "+pd.DisplayName);
     Print("input name = "+pd.Name);
     Print("input value = " + pd.GetValue(GetPropertyOwner(pd)));   
}

素敵な文字列を取得したいカスタム オブジェクトがある場合は、以下のようなヘルパー メソッドを追加できます。

    private string GetPropertyValue(PropertyDescriptor pd)
    {
        var property = GetPropertyOwner(pd);
        if (property is CustomObject)
        {
            var dataSeries = property as CustomObject;
            // This will return a string of the list contents ("One, Two, Three")
            return string.Join(",", dataSeries.ListProperty.ToArray());

        }
        else if (property is ....)
        {
            return somthing else
        }
        return property.ToString();
    }

デモクラス:

public class CustomObject
{
    private List<string> _listProperty = new List<string>(new string[]{"One","Two","Three"});
    public List<string> ListProperty
    {
        get { return _listProperty; }
        set { _listProperty = value; }
    }

}
于 2012-12-03T23:05:26.970 に答える