如何从属性窗口设置UserControl的自定义属性,这是一个类

时间:2016-08-28 10:19:36

标签: c# properties user-controls custom-controls

我创建了一个扩展PictureBox Control

的UserControl
public partial class AudioMonitor : PictureBox
{
    private SelectionSettings _selectionSettings;

    [Description("Various settings regarding to the selection visuals"), Category("Custom")]
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    public SelectionSettings SelectionSettings
    {
        get { return this._selectionSettings; }
        set { this._selectionSettings = value; }
    }
}

SelectionSettings属性是我创建的自定义类,如下所示:

[Serializable]
public class SelectionSettings
{
    private SelectionMarker _startMarker;
    private SelectionMarker _endMarker;
    private SelectionPen _selectionStyle;

    public SelectionMarker StartMarker
    {
        get { return this._startMarker; }
        set { this._startMarker = value; }
    }
    public SelectionMarker EndMarker
    {
        get { return this._endMarker; }
        set { this._endMarker = value; }
    }
    public SelectionPen SelectionStyle
    {
        get { return this._selectionStyle; }
        set { this._selectionStyle = value; }
    }
}

[Serializable]
public class SelectionMarker
{
    private Color _color = Color.White;
    private DashStyle _style = DashStyle.Solid;
    private float _width = 1.0F;

    public Color Color
    {
        get { return this._color; }
        set { this._color = value; }
    }
    public DashStyle Style
    {
        get { return this._style; }
        set { this._style = value; }
    }
    public float Width
    {
        get { return this._width; }
        set { this._width = value; }
    }
    public Pen Pen
    {
        get
        {
            Pen pen = new Pen(this._color);
            pen.DashStyle = this._style;
            pen.Width = this._width;

            return pen;
        }
    }
}

[Serializable]
public class SelectionPen
{
    private Color _color = Color.White;
    private DashStyle _style = DashStyle.Solid;
    private float _width = 1.0F;
    private float _alpha = 100;

    public Color Color
    {
        get { return this._color; }
        set { this._color = value; }
    }
    public DashStyle Style
    {
        get { return this._style; }
    }
    public float Width
    {
        get { return this._width; }
    }
    public float Alpha
    {
        get { return this._alpha; }
    }
    public int AlphaPercent
    {
        get { return (int)Math.Round(this._alpha * 100 / 255); }
        set
        {
            if (value > 0 && value <= 100)
                this._alpha = (value * 255 / 100);
            else
                throw new ArgumentException("Alpha percentage should be between (0, 100]");
        }
    }
    public Pen Pen
    {
        get
        {
            Pen pen = new Pen(Color.FromArgb((byte)this._alpha, this._color.R, this._color.G, this._color.B));
            pen.DashStyle = this._style;
            pen.Width = this._width;

            return pen;
        }
    }
}

当我将自定义控件放在窗体上并打开属性窗口时,我可以看到如下: enter image description here

如您所见,我无法在设计时从“属性”窗口设置“SelectionSettings”属性。我需要的是在属性名称旁边放置“...”按钮并打开一个弹出窗口来设置值。

看起来应该是这样的: enter image description here

我该如何完成这项任务?

1 个答案:

答案 0 :(得分:1)

您要做的是将Editor添加到AudioMonitor的{​​{1}}财产。

为此,您应该创建一个派生自UITypeEditor Class的自定义类。

为了从SelectionSettings继承,您的项目必须引用UITypeEditor,这可以通过转到“项目”菜单,选择“添加引用”以打开“引用管理器”,导航到“程序集” - 来完成。 ;左侧面板上的框架,并确保在列表中检查System.Design

在自定义System.Design派生类中,覆盖方法UITypeEditor以显示编辑EditValue类型值的自定义对话框。然后将SelectionSettings属性上的编辑器属性设置为自定义SelectionSettings派生类。

这是一个通用的代码示例,说明了它的外观:

UITypeEditor

您可能需要查找using System; using System.Drawing.Design; using System.ComponentModel; using System.Windows.Forms; // ... [Editor(typeof(SomeProperty_Editor), typeof(UITypeEditor))] // You might be able to place this attribute on class SomeType, but I haven't tried yet public SomeType SomeProperty { get { /* stuff */ } set { /* stuff */ } // optional, really } class SomeProperty_Editor : UITypeEditor { public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { return UITypeEditorEditStyle.Modal; } public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { IWindowsFormsEditorService service = (IWindowsFormsEditorService)(provider.GetService(typeof(IWindowsFormsEditorService))); SomeProperty_EditorWindow EditorWindow = new SomeProperty_EditorWindow(value as SomeType); service.ShowDialog(EditorWindow); if (EditorWindow.EditCancelled) return value; else return EditorWindow.GetEdittedValue(); } } class SomeProperty_EditorWindow : Form { public SomeProperty_EditorWindow(SomeType CurrentProperty) : base() { InitializeComponents(); // Grab info in CurrentProperty here and display it on form } public void InitializeComponents() { // write yourself or use designer } public SomeType GetEdittedValue() { // return editted value from form components } public bool EditCancelled = false; // Set true if cancel button hit } 控件,因为它在您的EditorWindow中非常有用。此外,您可以转到PropertyGrid Class的MSDN页面,查看&#34;继承层次结构&#34;下的所有.NET派生类。查看是否有一个内置编辑器,它的功能与您想要的功能相近,并直接从该类继承。