C# 动态设计时属性
C# dynamic design-time properties
我想要一个控件,允许在另一个 属性 的值设置为特定值时显示 属性。以下是我想要的一个简化得多的示例:
public class CustomButton : Control
{
private ButtonType _bType = ButtonType.OnOff;
private Int32 _minPress = 50; // 50 mS
public ButtonType Button_Type
{
get { return _bType; }
set { _bType = value; }
}
public Int32 Minimum_Press_Time // Only for momentary buttons
{
get { return _minPress; }
set { _minPress = value; }
}
}
public enum ButtonType
{
Momentary,
OnOff
}
将 CustomButton 添加到 Windows.Forms 表单时,如果 Button_Type 更改为 ButtonType.Momentary,Minimum_Press_Time 将仅显示在属性 window 中。
这样的事情可能吗?
是的,可以靠近但看起来有点奇怪。我以前在一些控件上做过这个。以下是您需要执行的操作的完整示例:
public partial class CustomButton : Control
{
private ButtonType _buttonType = ButtonType.OnOff;
private CustomButtonOptions _options = new OnOffButtonOptions();
[RefreshProperties(System.ComponentModel.RefreshProperties.All)]
public ButtonType ButtonType
{
get { return _buttonType; }
set
{
switch (value)
{
case DynamicPropertiesTest.ButtonType.Momentary:
_options = new MomentaryButtonOptions();
break;
default:
_options = new OnOffButtonOptions();
break;
}
_buttonType = value;
}
}
[TypeConverter(typeof(ExpandableObjectConverter))]
public CustomButtonOptions ButtonOptions
{
get { return _options; }
set { _options = value; }
}
public CustomButton()
{
InitializeComponent();
}
}
public enum ButtonType
{
Momentary,
OnOff
}
public abstract class CustomButtonOptions
{
}
public class MomentaryButtonOptions : CustomButtonOptions
{
public int Minimum_Press_Time { get; set; }
public override string ToString()
{
return Minimum_Press_Time.ToString();
}
}
public class OnOffButtonOptions : CustomButtonOptions
{
public override string ToString()
{
return "No Options";
}
}
基本上,您正在使用 ExpandableObjectConverter 将抽象类型转换为一组选项。然后,您使用 RefreshProperties 属性告诉 属性 网格它需要在此 属性 更改后刷新属性。
这是我发现的最接近您要求的最简单方法。 属性 网格并不总是以正确的方式刷新,因此有时在没有可扩展属性的选项集旁边会有一个“+”号。使用属性中的 "ToString" 使 属性 网格上的显示看起来更智能。
我想要一个控件,允许在另一个 属性 的值设置为特定值时显示 属性。以下是我想要的一个简化得多的示例:
public class CustomButton : Control
{
private ButtonType _bType = ButtonType.OnOff;
private Int32 _minPress = 50; // 50 mS
public ButtonType Button_Type
{
get { return _bType; }
set { _bType = value; }
}
public Int32 Minimum_Press_Time // Only for momentary buttons
{
get { return _minPress; }
set { _minPress = value; }
}
}
public enum ButtonType
{
Momentary,
OnOff
}
将 CustomButton 添加到 Windows.Forms 表单时,如果 Button_Type 更改为 ButtonType.Momentary,Minimum_Press_Time 将仅显示在属性 window 中。
这样的事情可能吗?
是的,可以靠近但看起来有点奇怪。我以前在一些控件上做过这个。以下是您需要执行的操作的完整示例:
public partial class CustomButton : Control
{
private ButtonType _buttonType = ButtonType.OnOff;
private CustomButtonOptions _options = new OnOffButtonOptions();
[RefreshProperties(System.ComponentModel.RefreshProperties.All)]
public ButtonType ButtonType
{
get { return _buttonType; }
set
{
switch (value)
{
case DynamicPropertiesTest.ButtonType.Momentary:
_options = new MomentaryButtonOptions();
break;
default:
_options = new OnOffButtonOptions();
break;
}
_buttonType = value;
}
}
[TypeConverter(typeof(ExpandableObjectConverter))]
public CustomButtonOptions ButtonOptions
{
get { return _options; }
set { _options = value; }
}
public CustomButton()
{
InitializeComponent();
}
}
public enum ButtonType
{
Momentary,
OnOff
}
public abstract class CustomButtonOptions
{
}
public class MomentaryButtonOptions : CustomButtonOptions
{
public int Minimum_Press_Time { get; set; }
public override string ToString()
{
return Minimum_Press_Time.ToString();
}
}
public class OnOffButtonOptions : CustomButtonOptions
{
public override string ToString()
{
return "No Options";
}
}
基本上,您正在使用 ExpandableObjectConverter 将抽象类型转换为一组选项。然后,您使用 RefreshProperties 属性告诉 属性 网格它需要在此 属性 更改后刷新属性。
这是我发现的最接近您要求的最简单方法。 属性 网格并不总是以正确的方式刷新,因此有时在没有可扩展属性的选项集旁边会有一个“+”号。使用属性中的 "ToString" 使 属性 网格上的显示看起来更智能。