编写了一个自定义控件,控件有一个属性,需要用下拉式属性来设置值,
//*************************************************************************************************//
[TypeConverter(typeof(ControlsConverter))] //指定一个转换器,就在下面
[DefaultValue("Normal")]
[Description("设置日期的选择范围")]
public string DateRange
{
get
{
object o = ViewState["DateRange"];
return (o == null) ? "" : o.ToString();
}
set
{
ViewState["DateRange"] = value;
}
}
[DefaultValue("Normal")]
[Description("设置日期的选择范围")]
public string DateRange
{
get
{
object o = ViewState["DateRange"];
return (o == null) ? "" : o.ToString();
}
set
{
ViewState["DateRange"] = value;
}
}
//*************************************************************************************************//
public class ControlsConverter : StringConverter
{
StandardValuesCollection theValue = new StandardValuesCollection(new string[] {"Before Today", "Normal", "After Today"});
//这一个override说明要用下拉列表编辑属性
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
//这个override返回下拉列表项
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return theValue;
{
StandardValuesCollection theValue = new StandardValuesCollection(new string[] {"Before Today", "Normal", "After Today"});
//这一个override说明要用下拉列表编辑属性
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
//这个override返回下拉列表项
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return theValue;
}
//return ture的话属性只能选,return flase 属性可选可填
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return true;
}
//return ture的话属性只能选,return flase 属性可选可填
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return true;
}
}