如何强制PropertyGrid始终显示下拉按钮?

时间:2013-08-01 18:53:03

标签: c# .net winforms propertygrid

对于枚举和其他类型,一旦选中,PropertyGrid将在右侧显示一个下拉按钮,表示可以单击该按钮进行更改。是否可以强制PropertyGrid始终显示此下拉按钮,就像它们是正常的ComboBox一样?

编辑:为了澄清,在下面的屏幕截图中,选中了当前设置为Color.Red的属性,因此您看到了下拉按钮,但当前设置为Color.Blue的属性不是,所以您看不到它。我想总是看到下拉按钮。

enter image description here

2 个答案:

答案 0 :(得分:2)

经过一些研究后,我发现你无法强制下拉按钮始终对属性网格中的每个属性都可见。原因是因为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() });
        }
    }

希望它有所帮助。 鞍本

答案 1 :(得分:1)

我无法解释所有发生的事情因为我只是深入到足以让它适用于一个特定的实例,但是......

给定一个类中的属性,您希望将其显示为下拉列表:

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并不是我的强项,但我希望这会有所帮助。