自定义控件与 PropertyGrid 配合不佳
我有一个正在实现自定义 ToolStripItem 的类。一切似乎都很顺利,直到我尝试在设计时将项目添加到 ContextMenuStrip 中。如果我尝试直接从 ContextMenuStrip 添加自定义控件,PropertyGrid 就会冻结,并且不允许我修改“Checked”或“Text”属性。但是,如果我进入 ContextMenuStrip PropertyGrid 并通过 Items(...)
属性添加自定义控件,我就可以在该对话框中很好地修改自定义控件。我不确定我是否在某个地方缺少某个属性,或者底层代码是否有问题。这是 CustomToolStripItem 类的副本。正如您所看到的,这是一个非常简单的类。
[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ContextMenuStrip)]
public class CustomToolStripItem : ToolStripControlHost {
#region Public Properties
[Description("Gets or sets a value indicating whether the object is in the checked state")]
[ReadOnly(false)]
public bool Checked { get { return checkBox.Checked; } set { checkBox.Checked = value; } }
[Description("Gets or sets the object's text")]
[ReadOnly(false)]
public override string Text { get { return checkBox.Text; } set { checkBox.Text = value; } }
#endregion Public Properties
#region Public Events
public event EventHandler CheckedChanged;
#endregion Public Events
#region Constructors
public CustomToolStripItem()
: base(new FlowLayoutPanel()) {
// Setup the FlowLayoutPanel.
controlPanel = (FlowLayoutPanel)base.Control;
controlPanel.BackColor = Color.Transparent;
// Add the child controls.
checkBox.AutoSize = true;
controlPanel.Controls.Add(checkBox);
}
#endregion Constructors
#region Protected Methods
protected override void OnSubscribeControlEvents(Control control) {
base.OnSubscribeControlEvents(control);
checkBox.CheckedChanged += new EventHandler(CheckChanged);
}
protected override void OnUnsubscribeControlEvents(Control control) {
base.OnUnsubscribeControlEvents(control);
checkBox.CheckedChanged -= new EventHandler(CheckChanged);
}
#endregion Protected Methods
#region Private Methods
private void CheckChanged(object sender, EventArgs e) {
// Throw the CustomToolStripItem's CheckedChanged event
EventHandler handler = CheckedChanged;
if (handler != null) { handler(sender, e); }
}
#endregion Private Methods
#region Private Fields
private FlowLayoutPanel controlPanel;
private CheckBox checkBox = new CheckBox();
#endregion Private Fields
I have a class that is implementing a custom ToolStripItem. Everything seems to work great until I try to add the item to a ContextMenuStrip at design time. If I try to add my custom control straight from the ContextMenuStrip the PropertyGrid freezes up and will not let me modify my Checked or Text properties. But if I go into the ContextMenuStrip PropertyGrid and add my custom control through the Items(...)
property, I can modify the custom control just fine within that dialog. I'm not sure if I'm missing an attribute somewhere of if its a problem with the underlying code. Here is a copy of the CustomToolStripItem class. As you can see, its a very simple class.
[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ContextMenuStrip)]
public class CustomToolStripItem : ToolStripControlHost {
#region Public Properties
[Description("Gets or sets a value indicating whether the object is in the checked state")]
[ReadOnly(false)]
public bool Checked { get { return checkBox.Checked; } set { checkBox.Checked = value; } }
[Description("Gets or sets the object's text")]
[ReadOnly(false)]
public override string Text { get { return checkBox.Text; } set { checkBox.Text = value; } }
#endregion Public Properties
#region Public Events
public event EventHandler CheckedChanged;
#endregion Public Events
#region Constructors
public CustomToolStripItem()
: base(new FlowLayoutPanel()) {
// Setup the FlowLayoutPanel.
controlPanel = (FlowLayoutPanel)base.Control;
controlPanel.BackColor = Color.Transparent;
// Add the child controls.
checkBox.AutoSize = true;
controlPanel.Controls.Add(checkBox);
}
#endregion Constructors
#region Protected Methods
protected override void OnSubscribeControlEvents(Control control) {
base.OnSubscribeControlEvents(control);
checkBox.CheckedChanged += new EventHandler(CheckChanged);
}
protected override void OnUnsubscribeControlEvents(Control control) {
base.OnUnsubscribeControlEvents(control);
checkBox.CheckedChanged -= new EventHandler(CheckChanged);
}
#endregion Protected Methods
#region Private Methods
private void CheckChanged(object sender, EventArgs e) {
// Throw the CustomToolStripItem's CheckedChanged event
EventHandler handler = CheckedChanged;
if (handler != null) { handler(sender, e); }
}
#endregion Private Methods
#region Private Fields
private FlowLayoutPanel controlPanel;
private CheckBox checkBox = new CheckBox();
#endregion Private Fields
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您不介意使用标准 ToolStripMenuItem,它应该具有您创建的自定义控件的所有功能。它已经具有“Text”和“Checked”属性以及事件处理程序,以及所有正常的附加功能,尽管 ToolStripMenuItem 的“checked”外观与常规复选框不同。
If you don't mind using the standard ToolStripMenuItem it should have all the functionality of the custom control you created. It has both "Text" and "Checked" properties and event handlers already, plus all the normal bells and whistles, although the "checked" look for a ToolStripMenuItem is different from a regular check box.