【问题标题】:How do I add an editable combobox to a System.Windows.Forms.PropertyGrid?如何将可编辑组合框添加到 System.Windows.Forms.PropertyGrid?
【发布时间】:2012-04-05 02:52:47
【问题描述】:

我有一个 System.Windows.Forms.PropertyGrid 具有不同类型的值。对于特定项目,我想显示一个可供选择的有用值列表。用户还可以键入新值。类似于传统下拉组合框的东西:

到目前为止,我有自己的System.ComponentModel.TypeConverter,但我不知道如何获得带有建议值的下拉列表直接编辑值的可能性。请帮忙!

【问题讨论】:

    标签: c# .net propertygrid typeconverter


    【解决方案1】:

    您可以通过实现自己的UITypeEditor 来完成此操作。

    我推荐阅读Getting the Most Out of the .NET Framework PropertyGrid Control。特别是,标题为Providing a Custom UI for Your Properties 的部分介绍了如何为特定属性制作自定义控件。

    【讨论】:

    • 继承System.ComponentModel.StringConverter 解决了这个问题。显然,文本编辑不能用字符串以外的其他类型来完成。不过感谢您的链接!
    【解决方案2】:

    这很容易。在你自己的StringConverter 中返回falseGetStandardValuesExclusive 就是这样。

    看这里:

    internal class cmbKutoviNagiba : StringConverter
    {
          public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
          {
              return FALSE;    // <----- just highlight! remember to write it lowecase
          }
    
          public override TypeConverter.StandardValuesCollection GetStandardValues(
              ITypeDescriptorContext context)
          {
              string[] a = { "0", "15", "30", "45", "60", "75", "90" };
              return new StandardValuesCollection(a);
          }
    
          public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
          {
              return true;
          }
      }
    

    我用大写字母写了FALSE,只是为了让你更容易看到它。请用小写:)

    【讨论】:

    • 顺便说一句:GetStandardValuesExclusive 的覆盖似乎只有在派生自 StringConverter 的类中使用时才会被调用。当您从 TypeConverter 派生类时,它似乎没有被调用。
    猜你喜欢
    • 1970-01-01
    • 2010-09-16
    • 2014-06-18
    • 2011-02-03
    • 1970-01-01
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多