如何在PropertyGridControl中合并ListBox?

时间:2013-02-28 08:56:31

标签: c# .net winforms

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

Sceenshot:

enter image description here

1 个答案:

答案 0 :(得分: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 to edit property value

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。享受结果:)。 enter image description here

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

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