2011-11-16 2 views
15

사용자 지정을 위해 PropertyGrid에서 디자인 타임과 런타임 모두에 사용되는 ICustomTypeDescriptor를 구현하는 클래스 (컨트롤)가 있습니다. 디자인 타임에 다른 속성을 노출해야합니다 (예 : width, height 등의 컨트롤 속성). 프로그램에서 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- run-by-visual-studio-designer) *. –

답변

10

DesignMode이 맞는지 확인하십시오. 컨트롤 기본 클래스에 속하는 속성입니다.

+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 기준의 속성을 사용하십시오. 모드에 대해 알려줍니다.

관련 문제