2010-03-30 76 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