1

我在一个应用程序中看到了这一点,只是想知道这是否可以使用 C# 在 winforms 中完成。我没有自定义 PropertyGrid 控件的经验,如果您能在代码上帮助我,我将不胜感激。提前致谢。

截图:

在此处输入图像描述

4

1 回答 1

2

有几个步骤可以做到这一点。

1. 为您的值定义枚举。

public enum TypeCodes
{
    Baking,
    Barbecuing,
    Blanching,
    Blind_Baking,
    Boiling,
    Broiling,
    Deep_Frying,
}

2. 创建具有 List 类型属性的类。

using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;

namespace PropertyGrid_ListBox_Test
{
    class TestCase
    {
        public TestCase()
        {
            Manufacturer = new List<TypeCodes>();
        }

        [DisplayName("Manufacturer Preparation Type Code")]
        public List<TypeCodes> Manufacturer { get; set; }
    }
}

3. 创建 UserControl - TypeCodeSelectionControl 将用于编辑此属性。

UserControl 编辑属性值

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace PropertyGrid_ListBox_Test
{
    public partial class TypeCodeSelectionControl : UserControl
    {
        public List<TypeCodes> List { get; set; }
        private IWindowsFormsEditorService editorService;

        public TypeCodeSelectionControl()
        {
            InitializeComponent();
        }

        private void PrepareListBoxes()
        {
            foreach (string myType in Enum.GetNames(typeof(TypeCodes)))
                listBox1.Items.Add(myType);

            foreach (TypeCodes type_code in this.List)
            {
                string enum_value = Enum.GetName(typeof(TypeCodes), type_code);
                listBox1.Items.Remove(enum_value);
                listBox2.Items.Add(enum_value);
            }
        }

        public TypeCodeSelectionControl(List<TypeCodes> list, IWindowsFormsEditorService editorService)
        {
            InitializeComponent();

            this.List = list;
            this.editorService = editorService;

            PrepareListBoxes();
        }

        private void buttonALL_Click(object sender, EventArgs e)
        {
            //
            // Add all items.
            //
            foreach (object obj in listBox1.Items)
            {
                listBox2.Items.Add(obj);
            }
            listBox1.Items.Clear();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            //
            // Add selected item.
            //
            string item_to_add = (string)listBox1.SelectedItem;
            listBox1.Items.Remove(item_to_add);
            listBox2.Items.Add(item_to_add);
        }

        private void buttonRemove_Click(object sender, EventArgs e)
        {
            //
            // Remove selected item.
            //
            string item_to_remove = (string)listBox2.SelectedItem;
            listBox2.Items.Remove(item_to_remove);
            listBox1.Items.Add(item_to_remove);
        }

        private void buttonNone_Click(object sender, EventArgs e)
        {
            //
            // Remove all items.
            //
            foreach (object obj in listBox2.Items)
            {
                listBox1.Items.Add(obj);
            }
            listBox2.Items.Clear();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //
            // Enable "Add" button only if any of items is selected in the left listbox.
            //
            buttonAdd.Enabled = listBox1.SelectedIndex != -1;
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            //
            // Enable "Remove" button only if any of items is selected in the right listbox.
            //
            buttonRemove.Enabled = listBox2.SelectedIndex != -1;
        }

        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);

            //
            // This occures when dropdown UI editor is hiding.
            //
            if (this.Visible == false)
            {
                //
                // Clear previously selected items.
                //
                if (this.List == null)
                {
                    this.List = new List<TypeCodes>();
                }
                else
                {
                    this.List.Clear();
                }

                //
                // Fill the list with currently selected items.
                //
                foreach (string obj in listBox2.Items)
                {
                    this.List.Add((TypeCodes)Enum.Parse(typeof(TypeCodes), obj));
                }
                editorService.CloseDropDown();
            }
        }
    }
}

4. 创建 UITypeEditor - TypeCodeEditor 将处理先前创建的 UserControl。

using System;
using System.Collections.Generic;
using System.Drawing.Design;
using System.Windows.Forms.Design;

namespace PropertyGrid_ListBox_Test
{
    class TypeCodeEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }

        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            IWindowsFormsEditorService editorService = null;
            if (provider != null)
            {
                editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
            }

            if (editorService != null)
            {
                TypeCodeSelectionControl selectionControl = new TypeCodeSelectionControl((List<TypeCodes>)value, editorService);
                editorService.DropDownControl(selectionControl);

                value = selectionControl.List;
            }

            return value;
        }
    }
}

5. 创建 TypeConverter 以正确显示我们的属性值。

using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace PropertyGrid_ListBox_Test
{
    class TypeCodeTypeConverter : TypeConverter
    {
        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)
            {
                List<TypeCodes> list = new List<TypeCodes>();
                string[] values = ((string)value).Split(new char[] { ';' });
                foreach (string v in values)
                {
                    list.Add((TypeCodes)Enum.Parse(typeof(TypeCodes), v));
                }

                return list;
            }
            return base.ConvertFrom(context, culture, value);
        }

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

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                List<TypeCodes> list = (List<TypeCodes>)value;
                string result = "";
                foreach (TypeCodes type_code in list)
                {
                    result += Enum.GetName(typeof(TypeCodes), type_code) + "; ";
                }
                return result;
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }
}

6. 为属性添加适当的属性,使其可以使用我们的下拉自定义编辑器进行编辑。

using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;

namespace PropertyGrid_ListBox_Test
{
    class TestCase
    {
        public TestCase()
        {
            Manufacturer = new List<TypeCodes>();
        }

        [DisplayName("Manufacturer Preparation Type Code")]
        [Editor(typeof(TypeCodeEditor), typeof(UITypeEditor))]
        [TypeConverter(typeof(TypeCodeTypeConverter))]
        public List<TypeCodes> Manufacturer { get; set; }
    }
}

7.享受结果:)。 在此处输入图像描述

我希望这段代码能帮助您继续编写自己的自定义编辑器。

此处提供完整示例:https ://github.com/virious/PropertyGrid_CustomDropDownEditor 。

于 2013-03-04T21:07:43.740 回答