简体   繁体   中英

WinForms user control Load event and access to persisted properties at design time

I created a user control, where I am providing design-time support. I expose appropriate properties and my required child controls are being correctly persisted in the parent form that contains the user control.

In the form's load event, I need to re-construct certain child controls from the "MenuItems" property (see code below) that got persisted. The Load event fires correctly every time the form is opened in Visual Studio IDE. The first time the form is opened the collection correctly contains all expected items. However, when I open the form again later in the same Visual Studio session, the collection is empty. The Load event fires, but the persisted collection is empty. If I close Visual Studio and open the form again, the collection correctly shows the expected number of items again.

Below is the relevant code. I am using a 3rd party DevExpress NavBarControl, where I rebuild the menu items from a persisted collection. The items are code generated into the parent's form (MenuItems is the collection that gets persisted):

public partial class MyUserControl : UserControl
{
    private List<NavBarItem> menuItems = new List<NavBarItem>(15);

    [Browsable(false),
       DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public List<NavBarItem> MenuItems
    {
       get
       {
           return this.menuItems;
       }
    }

    public MyUserControl()
    {
        InitializeComponent();
    }

    private void NavOptionsControl_Load(object sender, EventArgs e)
    {
        foreach (NavBarItem item in this.menuItems)
        {
            NavBarItemLink link = new NavBarItemLink(item);
            this.navBarGroup.ItemLinks.Add(item);
            item.LinkClicked += new NavBarLinkEventHandler(NavBarItem_LinkClicked);
        }
    }
}

Have you tried the layout event? or possibly the paint event?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM