【发布时间】: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