У меня есть класс, который реализует пользовательский ToolStripItem. Кажется, все работает отлично, пока я не попытаюсь добавить элемент в ContextMenuStrip во время разработки. Если я пытаюсь добавить свой пользовательский элемент управления прямо из ContextMenuStrip, PropertyGrid зависает и не позволяет мне изменять свойства Checked или Text. Но если я захожу в PropertyMrid ContextMenuStrip и добавляю свой пользовательский элемент управления через свойство 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