【问题标题】:How to set UserControl's Custom Property, which is a class, from Properties Window如何从属性窗口设置 UserControl 的自定义属性,这是一个类
【发布时间】:2016-08-28 10:19:36
【问题描述】:

我创建了一个扩展 PictureBox 控件的用户控件

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;
        }
    }
}

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

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

它应该看起来像这样:

我怎样才能完成这项任务?

【问题讨论】:

  • 我现在正在尝试查找参考资料,因为去年我遇到了类似的问题。本质上,您的课程必须序列化/重新序列化为字符串。正如您所看到的字体,它列出了选定的字体名称和大小,并带有 ;分隔符。找到后会立即发布示例。
  • 您的 SelectionSettings 需要有一个类型转换器,没有这个属性将显示为灰色。转换器需要继承自 ExpandableObjectConverter。你可以看到这篇文章msdn.microsoft.com/en-us/library/aa302326.aspx 并向下滚动到显示复杂属性部分。
  • 非常感谢您的回复。但据我所知,本文档解释了如何创建 PropertyGrid 以在运行时更改值。但我想在设计时在属性窗口中插入此功能。我只是快速浏览了文档,可能会遗漏一些内容,如果我错了,请纠正我。
  • 回到绘图板!看看我能想出什么:)
  • 据我所见,它在 VS 设计时属性窗口中使用了相同的 TypeConverter 模式。请参阅此问题的第二个答案:stackoverflow.com/questions/13016992/…

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


【解决方案1】:

您要做的是将Editor 添加到AudioMonitorSelectionSettings 属性。

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

为了从UITypeEditor继承,您的项目必须引用System.Design,这可以通过转到项目菜单,选择添加引用以打开引用管理器,导航到左侧的Assemblies->Framework来完成面板并确保在列表中选中System.Design

在您的自定义UITypeEditor 派生类中,覆盖方法EditValue 以显示一个自定义对话框,该对话框编辑SelectionSettings 类型的值。然后将 SelectionSettings 属性上的 Editor Attribute 设置为自定义的 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
}

您可能需要查找PropertyGrid 控件,因为它在您的EditorWindow 中可能非常有用。此外,您可以转到UITypeEditor Class 的 MSDN 页面,查看“继承层次结构”下的所有 .NET 派生类,看看是否有一个内置编辑器,它的功能与您想要做的事情和继承很接近直接来自那个班级。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-21
    • 2010-09-09
    • 2012-03-16
    • 2014-03-21
    • 2010-10-25
    • 1970-01-01
    • 2021-05-16
    • 1970-01-01
    相关资源
    最近更新 更多