[英]How to know whether a control is at design-time or not?
我有一個類(控件),實現了ICustomTypeDescriptor,它在設計時和PropertyGrid運行時都用於自定義。 我需要在設計時公開不同的屬性(標准控件屬性,如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#中的設計時標志是否有模擬? 使用條件編譯可能更好嗎?
檢查DesignMode是true還是false。 它是屬於控件基類的屬性。
標志應該是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文檔 。
使用基礎的DesignMode
屬性。 這將告訴您有關模式的信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.