PropertyGrid控件和下拉列表 [英] PropertyGrid control and drop-down lists

查看:287
本文介绍了PropertyGrid控件和下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个下拉列表的编辑器的属性;如果我只有字符串作为下拉列表中的条目,这将正常工作(使用StringConverter)。然而,当我试图使用对象而不是字符串列表,这是行不通的(注意它是如何工作的正常组合框但是!)
这是我的代码:

I wanted to create a drop-down list as the editor for a property; If I had only strings as entries for the dropdown list, this would work fine (Using a StringConverter). However, when I tried to use a list of objects instead of Strings, this would not work (note how it works for normal comboboxes however!) This was my code:

    public static List<Bar> barlist;
    public Form1()
    {
        InitializeComponent();
        barlist = new List<Bar>();
        for (int i = 1; i < 10; i++)
        {
            Bar bar = new Bar();
            bar.barvalue = "BarObject " + i;
            barlist.Add(bar);
            comboBox1.Items.Add(bar);
        }
        Foo foo = new Foo();
        foo.bar = new Bar();
        propertyGrid1.SelectedObject = foo;
    }

    public class Foo
    {
        Bar mybar;

        [TypeConverter(typeof(BarConverter))]
        public Bar bar
        {
            get { return mybar; }
            set { mybar = value; }
        }
    }

    public class Bar
    {
        public String barvalue;
        public override String ToString()
        {
            return barvalue;
        }
    }


    class BarConverter : TypeConverter
    {
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            return new StandardValuesCollection(barlist);
        }
    }



结果(嵌入表单等)看起来像这样的:

The result (embedded into a form etc) looked like this:

点击一个条目给了我这样的:

Clicking an entry gave me this:

(很抱歉的德语文本,我不知道如果我可以改变这种状况,我是VS英语但我的OS心不是,该错误信息是

(Sorry about the german text, I am not sure if I can change that, my VS is english but my OS isnt, the error message is

  Invalid property value.

  The object of type "System.String" cannot be converted to type 
  "XMLTest.Form1+Bar".

我敢肯定,我可以通过定义向后转换工具,将字符串转换回一个Bar对象做这样的解决办法。这需要的按键是为了使这项工作正常一样。是否有这样做的更好的方式?为什么嵌入到PropertyGrid控件使用字符串(正常组合框有任何处理这个没有问题)

I am pretty sure I can do a workaround for this by defining backwards conversion tools that convert a string back into a Bar object. This would require the keys being different in order to make this work properly. Is there a nicer way of doing this? Why does the comboBox that is embedded into the propertyGrid control use Strings (the normal comboBox has no problems whatsoever dealing with this)

在最重要的是下拉框中,我可以改变位置中间分隔编程的?我还没有找到该选项。

On top of that, can I change the position of the middle separator programmatically? I have yet to find that option.

感谢您。

推荐答案

我添加了 CanConvertFrom ConvertFrom 方法来转换类:

I added the CanConvertFrom and ConvertFrom methods to your conversion class:

class BarConverter : TypeConverter
{
  public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
  {
    return true;
  }

  public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
  {
    return new StandardValuesCollection(barlist);
  }

  public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  {
    if (sourceType == typeof(string))
    {
      return true;
    }
    return base.CanConvertFrom(context, sourceType);
  }

  public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
  {
    if (value is string)
    {
      foreach (Bar b in barlist)
      {
        if (b.barvalue == (string)value)
        {
          return b;
        }
      }
    }
    return base.ConvertFrom(context, culture, value);
  }
}

这篇关于PropertyGrid控件和下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆