【问题标题】:WinForms PropertyGrid: dynamic StandardValuesCollection changingWinForms PropertyGrid:动态 StandardValuesCollection 变化
【发布时间】:2013-08-06 07:19:57
【问题描述】:

我想在 PropertyGrid 中实现自动完成字符串字段,可以设置为自定义值。

这是我的字符串转换器

public class EntityNameAutocompleteConverter : StringConverter
{
    public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
    {
        return true;
    }

    public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
    {
        return false;
    }

    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
        return new StandardValuesCollection(Globals.EntityCache.Select(e => e.Name).ToList());
    }
}

我将它设置为 TypeConverter 以便编辑字符串属性。

问题是可能有很多标准值。所以我想通过输入过滤它们,例如如果我输入了“Foo”,我将只看到从下拉列表中的“Foo”开始的字符串。

这有可能吗?也许可以从上下文或以任何其他方式获取属性的中间值?

【问题讨论】:

    标签: c# .net winforms propertygrid


    【解决方案1】:

    您可以使用context 参数并获取当前属性值,如下所示:

    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
        // get the current property value
        string value = (string)context.PropertyDescriptor.GetValue(context.Instance);
        return new StandardValuesCollection(GetFilteredList(value));
    }
    

    【讨论】:

    • 谢谢,效果很好。实际上,当您按下下拉按钮时,文本框会失去焦点,因此当前编辑值会保存在实例中。
    • 是的,如果你需要一个自定义的行为,你必须编写一个自定义的 UITypeEditor,像这样:stackoverflow.com/questions/4305033/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 2013-10-24
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多