对于枚举和其他类型,一旦选中,PropertyGrid 将在右侧显示一个下拉按钮,表示可以单击进行更改。是否可以强制 PropertyGrid 一直显示这个下拉按钮,就像它们是普通的 ComboBox 一样?
编辑:为了澄清,在下面的屏幕截图中,当前设置为 Color.Red 的属性被选中,因此您会看到下拉按钮,但当前设置为 Color.Blue 的属性没有,因此您看不到它。我希望始终看到下拉按钮。
对于枚举和其他类型,一旦选中,PropertyGrid 将在右侧显示一个下拉按钮,表示可以单击进行更改。是否可以强制 PropertyGrid 一直显示这个下拉按钮,就像它们是普通的 ComboBox 一样?
编辑:为了澄清,在下面的屏幕截图中,当前设置为 Color.Red 的属性被选中,因此您会看到下拉按钮,但当前设置为 Color.Blue 的属性没有,因此您看不到它。我希望始终看到下拉按钮。
经过一些研究,我发现您不能强制下拉按钮对属性网格中的每个属性始终可见。原因是propertygrid 只包含1 个下拉按钮。所以不能有多个下拉按钮同时可见。像枚举这样的属性的一种解决方案是在选择枚举属性时自动单击下拉按钮,这样您就不必选择属性行然后自己单击下拉按钮(这样可以节省一键!: ))。这是一个示例代码:
void propertyGrid1_SelectedGridItemChanged(object sender, SelectedGridItemChangedEventArgs e)
{
if (e.NewSelection != null &&
e.NewSelection.PropertyDescriptor != null &&
e.NewSelection.PropertyDescriptor.Converter != null &&
e.NewSelection.PropertyDescriptor.Converter.GetStandardValuesExclusive() == true)
{
System.Reflection.FieldInfo field_gridView = propertyGrid1.GetType().GetField("gridView", System.Reflection.BindingFlags.NonPublic
| System.Reflection.BindingFlags.Instance);
object gridView = field_gridView.GetValue(propertyGrid1);
System.Reflection.MethodInfo gridView_methode_get_DropDownButton = gridView.GetType().GetMethod("get_DropDownButton", System.Reflection.BindingFlags.NonPublic
| System.Reflection.BindingFlags.Instance);
object dropdownbutton = gridView_methode_get_DropDownButton.Invoke(gridView, new object[0]);
System.Reflection.MethodInfo dropdownbutton_method_OnClick = dropdownbutton.GetType().GetMethod("OnClick", System.Reflection.BindingFlags.NonPublic
| System.Reflection.BindingFlags.Instance);
dropdownbutton_method_OnClick.Invoke(dropdownbutton, new object[] { new System.EventArgs() });
}
}
希望能帮助到你。安本
我无法解释正在发生的所有事情,因为我只深入到足以使其适用于一个特定实例,但是......
给定类中的一个属性,您希望将其显示为下拉列表:
private String _groupHeader = null;
[Category("Display")]
[DisplayName("Group Header")]
[Description("Any adjacent columns sharing this text will be displayed together as a merged header.")]
[DefaultValue("")]
[TypeConverter(typeof(GroupHeaderTypeConverter))]
public String GroupHeader { get { return _groupHeader; } set { _groupHeader = value; } }
然后你需要一个StringConverter
:
public class GroupHeaderTypeConverter : StringConverter
{
private static List<String> groupHeaderList = new List<String>();
public override Boolean GetStandardValuesSupported(ITypeDescriptorContext context) { return true; }
public override Boolean GetStandardValuesExclusive(ITypeDescriptorContext context) { return false; }
public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { return new StandardValuesCollection(groupHeaderList); }
public static void SetList(List<String> newList) { groupHeaderList = newList; }
}
然后,在您的代码中,您必须设置列表:
private List<String> _groupHeaders = new List<String>();
//add to _groupHeaders
GroupHeaderTypeConverter.SetList(_groupHeaders);
_groupHeaders
就我而言,当我从数据库中提取数据时,我正在加载列表。
恐怕这就是我能做到的了……TypeDescriptor
这不是我的强项,但我希望这会有所帮助。