Using custom color picker dialog in PropertyGrid

人盡茶涼 提交于 2019-12-03 21:00:43
Jaex

I managed to use my custom color picker dialog in property grid and copying code of it here in case some need it too:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace HelpersLib
{
    public class MyColorEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (value.GetType() != typeof(RGBA))
            {
                return value;
            }

            IWindowsFormsEditorService svc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

            if (svc != null)
            {
                using (DialogColor form = new DialogColor((RGBA)value))
                {
                    if (svc.ShowDialog(form) == DialogResult.OK)
                    {
                        return form.NewColor.RGBA;
                    }
                }
            }

            return value;
        }

        public override bool GetPaintValueSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override void PaintValue(PaintValueEventArgs e)
        {
            using (SolidBrush brush = new SolidBrush((RGBA)e.Value))
            {
                e.Graphics.FillRectangle(brush, e.Bounds);
            }

            e.Graphics.DrawRectangleProper(Pens.Black, e.Bounds);
        }
    }
}

And this is how it looks in property grid:

When i click button of it, it will open custom color dialog.

But still have one problem which i can't solve. I can't use Color struct with this UITypeEditor, therefore created RGBA class. When i use color struct, it look like this:

I will open another question for it i guess: Custom ColorEditor does not work properly on Color struct

To interact with PropertyGrid, you have to create your own "property class" (as described here). You can customise different parts and thus there are multiple solutions for what you want. As a first approach to your problem, here you have a code for propertyGrid1:

Property curProperty = new Property();
propertyGrid1.SelectedObject = curProperty;

Where Property is defined by:

public class Property
{
    private ColorDialog _dialog = new customColorDialogDialog();

    public ColorDialog dialog
    {
        get { return _dialog; }
        set { _dialog.ShowDialog(); }
    }
}
class customColorDialogDialog : ColorDialog
{

}

In this code, your color dialog (customColorDialogDialog) is triggered when clicking on the cell on the right hand side of the property name ("dialog").

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!