gpt4 book ai didi

c# - 自定义控件与 PropertyGrid 不兼容

转载 作者:太空宇宙 更新时间:2023-11-03 16:54:48 25 4
gpt4 key购买 nike

我有一个实现自定义 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

最佳答案

如果您不介意使用标准的 ToolStripMenuItem,它应该具有您创建的自定义控件的所有功能。尽管 ToolStripMenuItem 的“已选中”外观与常规复选框不同,但它已经具有“Text”和“Checked”属性和事件处理程序,以及所有正常的附加功能。

关于c# - 自定义控件与 PropertyGrid 不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2546752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com