C#如何实现定义PropertyGrid下拉框并动态改变其值
我在网上找了很多办法,有些说继承与TypeConverter 然后重写
public class DropDownItem:TypeConverter
{
......
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return new StandardValuesCollection(new string[] { "1", "2", "3" });
}
}
最后在[CategoryAttribute("坐标"),TypeConverter(typeof(PropertyGridTable.DropDownItem))]
但是这样虽然成功产生下拉框,却是写死了的,不能动态地改变下拉框的值,请问大家知道该如何实现不
[解决办法]
再重写的方法体里写动态实现代码
[解决办法]
http://blog.csdn.net/akron/article/details/2750566
http://topic.csdn.net/u/20100827/11/5524219a-4457-4921-b8f2-b4c63bc6b016.html
[解决办法]
GetStandardValues方法中有个ITypeDescriptorContext参数,它可以给出当前编辑属性的上下文。
可以通过它来得到属性的拥有对象,并根据对象进行动态改变:
private void Form1_Load(object sender, EventArgs e)
{
this.propertyGrid1.SelectedObject = new My();
}
public class My
{
public int Count { get; set; }
[CategoryAttribute("坐标"), TypeConverter(typeof(MyNameConverter))]
public string Name { get; set; }
private class MyNameConverter : StringConverter
{
public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
if (context != null && context.Instance is My)
{
List<string> values = new System.Collections.Generic.List<string>();
values.Add("改变Count,改变列表");
for (int i = 0; i < (context.Instance as My).Count; i++)
{
values.Add((i + 1).ToString());
}
return new StandardValuesCollection(values);
}
return base.GetStandardValues(context);
}
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
}
}