自定义控件与 PropertyGrid 配合不佳

发布于 2024-08-27 15:33:05 字数 2355 浏览 4 评论 0原文

我有一个正在实现自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情愿 2024-09-03 15:33:05

如果您不介意使用标准 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文