简体   繁体   English

WinForms用户控件Load事件和在设计时访问持久属性

[英]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. 在表单的load事件中,我需要从持久化的“ MenuItems”属性(请参见下面的代码)中重构某些子控件。 The Load event fires correctly every time the form is opened in Visual Studio IDE. 每次在Visual Studio IDE中打开表单时,都会正确触发Load事件。 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. 但是,稍后稍后在同一Visual Studio会话中再次打开该窗体时,该集合为空。 The Load event fires, but the persisted collection is empty. 触发Load事件,但是持久化集合为空。 If I close Visual Studio and open the form again, the collection correctly shows the expected number of items again. 如果我关闭Visual Studio并再次打开该窗体,则该集合将再次正确显示期望的项目数。

Below is the relevant code. 以下是相关代码。 I am using a 3rd party DevExpress NavBarControl, where I rebuild the menu items from a persisted collection. 我正在使用第三方DevExpress NavBarControl,在这里我从持久化集合中重建菜单项。 The items are code generated into the parent's form (MenuItems is the collection that gets persisted): 这些项目是生成到父级表单中的代码(MenuItems是持久化的集合):

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? 还是油漆事件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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