C#如何实现定义PropertyGrid下拉框并动态改变其值

LXP 2011-12-09 04:55:22
我在网上找了很多办法,有些说继承与TypeConverter 然后重写
public class DropDownItem:TypeConverter
{
......
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return new StandardValuesCollection(new string[] { "1", "2", "3" });
}
}
最后在[CategoryAttribute("坐标"),TypeConverter(typeof(PropertyGridTable.DropDownItem))]

但是这样虽然成功产生下拉框,却是写死了的,不能动态地改变下拉框的值,请问大家知道该如何实现不
...全文
2247 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
gomoku 2011-12-15
  • 打赏
  • 举报
回复
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;
}
}
}
LXP 2011-12-15
  • 打赏
  • 举报
回复
就像有一个列表,点击列表中的元素,就可以显示元素的属性,而元素的属性中有些可以通过下拉框选择那种
LXP 2011-12-15
  • 打赏
  • 举报
回复
已经成功解决了,谢谢了
特别 2011-12-10
  • 打赏
  • 举报
回复
你动态怎么个动态法,什么条件下动态成什么?
光一个动态鬼知道了
姣游天下 2011-12-10
  • 打赏
  • 举报
回复
再重写的方法体里写动态实现代码

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧