如何将可编辑的组合框添加到System.Windows.Forms.PropertyGrid?

l33*_*33t 6 .net c# propertygrid typeconverter

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

在此输入图像描述

到目前为止,我有自己的System.ComponentModel.TypeConverter,但我无法弄清楚如何获得建议值的下拉列表直接编辑值的可能性.请帮忙!

Ree*_*sey 7

您可以通过实现自己的UITypeEditor来实现此目的.

我建议阅读" 充分利用.NET Framework PropertyGrid控件".特别是,标题为为您的属性提供自定义UI的部分介绍如何为特定属性进行自定义控件.

  • 继承`System.ComponentModel.StringConverter`解决了这个问题.显然,文本编辑不能用字符串以外的其他类型完成.谢谢你的链接! (2认同)

小智 6

这很容易.在你自己的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;
      }
  }
Run Code Online (Sandbox Code Playgroud)

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

  • 顺便说一下:`GetStandardValuesExclusive`的重写似乎只是在从`StringConverter`派生的类中使用时才被调用.当你从`TypeConverter`派生你的类时似乎没有被调用. (2认同)