属性网格属性上创建新实例 [英] Property Grid create new instance on a property

查看:157
本文介绍了属性网格属性上创建新实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立使用内置的Windows.Forms的PropertyGrid中快速的管理界面。我设法来装饰我的数据类与相应的属性(ExpandableObjectConverter等)和所有似乎很好地工作。

I'm trying to build a quick administrative interface using the built in Windows.Forms PropertyGrid . I managed to decorate my data classes with the appropriate attributes (ExpandableObjectConverter etc.) and all seems to work fine.

有一个用例,我没有搞清楚:当我对复杂属性设置值按钮出现,我可以编辑的内容,但是当我有一个空值似乎有没有办法来创建一个新的扩张实例所需类型的。  因此,一个解决方案,这将是很大的帮助。增加奖金,如果有人知道的一种方式,以present下拉到什么类型的它可以创建可能的派生值的列表的用户。

There is a use case I'm not figuring out: When i have values set on complex properties the expand button appears and i can edit the content but when i have a null value there seems to be no way to create a new instance of the desired type. So a solution to this would be of great help. Added bonus if someone knows of a way to present a drop-down to the user of what types it can create from a list of possible derived values.

推荐答案

这是没有那么复杂,下面是一个示例code,做这样的事情:

This is not so complicated, here is a sample code that does this kind of thing:

    public class MyEditor : UITypeEditor
    {
        private IWindowsFormsEditorService _editorService;

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (value != null) // already initialized
                return base.EditValue(context, provider, value);

            _editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            ListBox lb = new ListBox();
            lb.SelectionMode = SelectionMode.One;
            lb.SelectedValueChanged += OnListBoxSelectedValueChanged;

            // TODO: add your items/logic here
            lb.Items.Add(typeof(TYPE1));
            lb.Items.Add(typeof(TYPE2));
            ....
            lb.Items.Add(typeof(TYPEX));

            _editorService.DropDownControl(lb);
            if (lb.SelectedItem == null)
                return base.EditValue(context, provider, value); // no selection, no change

            // instantiate an object (add constructor logic if neede)
            return Activator.CreateInstance((Type)lb.SelectedItem);
        }

        private void OnListBoxSelectedValueChanged(object sender, EventArgs e)
        {
            _editorService.CloseDropDown();
        }
    }

这篇关于属性网格属性上创建新实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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