我有一个实现自定义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
答案 0 :(得分:0)
如果您不介意使用标准ToolStripMenuItem,它应该具有您创建的自定义控件的所有功能。它已经具有“Text”和“Checked”属性以及事件处理程序,加上所有正常的铃声和口哨声,尽管ToolStripMenuItem的“checked”查找与常规复选框不同。