2010-03-30 52 views
2

我有一个实现自定义ToolStripItem的类。在设计时尝试将项目添加到ContextMenuStrip之前,似乎一切都很顺利。如果我尝试直接从ContextMenuStrip添加自定义控件,则PropertyGrid会冻结并且不会让我修改Checked或Text属性。但是如果我进入ContextMenuStrip PropertyGrid并通过Items(...)属性添加我的自定义控件,我可以在该对话框中很好地修改自定义控件。我不确定如果我在某个地方遗漏了某个属性,如果它存在底层代码的问题。这里是CustomToolStripItem类的一个副本。正如你所看到的,它是一个非常简单的课程。自定义控件与PropertyGrid不兼容

[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

如果您不介意使用标准ToolStripMenuItem,它应该具有您创建的自定义控件的所有功能。它既具有“Text”和“Checked”属性和事件处理程序,也具有所有常规的钟声和哨声,但ToolStripMenuItem的“checked”外观与常规复选框不同。

+0

这并不给我我的其他自定义控件给我的标准外观。我真的很想知道是否有'ToolStripControlHost'发生了一些古怪的事情,或者如果我在我的代码中丢失了某些东西。 – lumberjack4 2010-03-30 21:19:40