C# 如何知道控件是否在设计时?
我有一个类(控件),实现ICustomTypeDescriptor,PropertyGrid在设计时和运行时都使用它进行定制。我需要在设计时(标准控件属性,如C# 如何知道控件是否在设计时?,c#,.net,design-time,C#,.net,Design Time,我有一个类(控件),实现ICustomTypeDescriptor,PropertyGrid在设计时和运行时都使用它进行定制。我需要在设计时(标准控件属性,如width、height等)和运行时(在程序中使用PropertyGrid更改该控件的其他属性时)公开不同的属性 我的代码如下: class MyControl : UserControl, ICustomTypeDescriptor { //Some code.. public PropertyDescriptorCol
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
。因此,您的代码应该如下所示
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;
}
}
这是相应的答案。检查正确与否。它是属于控件基类的属性。使用基类的属性。这将告诉您有关模式的信息 您是在谈论wpf还是winform?可能是的副本。实际上,它属于
System.ComponentModel.Component
基类。