2011-11-16 5 views
15

私はICustomTypeDescriptorを実装するクラス(コントロール)を持っています。これは、カスタマイズのためにPropertyGridによってデザインタイムとランタイムの両方で使用されます。私は、設計時(標準コントロールのプロパティ、例えばwidthheightなど)、実行時にPropertyGridをプログラムで使用して、そのコントロールの他のプロパティを変更する必要があります。コントロールがデザイン時かどうかを確認する方法?

私のコードは次のようである:

class MyControl : UserControl, ICustomTypeDescriptor 
{ 
    //Some code.. 

    public PropertyDescriptorCollection GetProperties(Attribute[] attributes) 
    { 
     return GetProperties(); 
    } 

    public PropertyDescriptorCollection GetProperties() 
    { 
     //I need to do something like this: 
     if (designTime) 
     { //Expose standart controls properties 
      return TypeDescriptor.GetProperties(this, true); 
     } 
     else 
     { 
      //Forming a custom property descriptor collection 
      PropertyDescriptorCollection pdc = new PropertyDescriptorCollection(null); 
      //Some code.. 
      return pdc; 
     } 
    } 
} 

は、C#でデザイン時のフラグのためのアナログはありますか?条件付きコンパイルを使用する方が良いでしょうか?

+1

あなたはwpfまたはwinformについて話していますか? –

+0

* [Visual Studioデザイナーが.NETコードを実行しているかどうかを確認する方法](http://stackoverflow.com/questions/73515/how-to-tell-if-net-code-is-being-ビジュアルスタジオデザイナー)*。 –

答えて

10

DesignModeがtrueまたはfalseであることを確認してください。これはコントロール基底クラスに属するプロパティです。

+2

実際、それは 'System.ComponentModel.Component'基底クラスに属します。 – tafa

8

フラグはDesignModeである必要があります。したがって、あなたのコードは、ここで

public PropertyDescriptorCollection GetProperties() 
{ 
    //I need to do something like this: 
    if (this.DesignMode) 
    { //Expose standart controls properties 
     return TypeDescriptor.GetProperties(this, true); 
    } 
    else 
    { //Forming a custom property descriptor collection 
     PropertyDescriptorCollection pdc = new PropertyDescriptorCollection(null); 
     //Some code.. 
     return pdc;  
    } 
} 

は応じMSDN docで次のようになります。

3

ベースのプロパティDesignModeを使用します。これはモードについて教えてくれるでしょう。

関連する問題