用户控件在设计时以编程方式添加另一个用户控件

发布于 2024-11-03 14:28:00 字数 1109 浏览 3 评论 0原文

我有一个用户控件(UC1),它可以在设计时根据用户想要显示的内容更改方面。

  • 一个常规按钮,弹出一个带有用户控件 UC2 的窗口(该窗口仅在运行时显示)
  • UC2 直接托管在 UC1 中(然后不显示常规按钮)

由于我想在这两种情况下使用相同的 UC2 实例,因此我只需转移UC1 和表单之间的所有权。

public UC1 ()
{
    _uc2 = new UC2 ();
}

public bool DisplayModeSimple
{
    get { return _displayModeSimple; }
    set
    {
        _displayModeSimple = value;
        if (_displayModeSimple)
        {
            // ... Verify if _uc2 is already in Controls...
            Controls.Remove (_uc2);
            uiButton.Visible = true;
        }
        else
        {
            // ... Verify that _uc2 is not in Controls ...
            Controls.Add (_uc2);
            uiButton.Visible = false;
        }
    }
}

private void HandleButtonClick (object sender, EventArgs e)
{
    // Not called if DisplayModeSimple=false since button is hidden...
    using (var form = new PopupForm (_uc2))
    {
        form.ShowDialog (this);
    }
}

在设计和运行时模式下都能正常工作。

在设计模式下,如果我更改显示模式 UC1 会正常运行。

但是,UC2 上的控件可以像运行时一样单击。 如果我随后关闭托管 UC1 的表单并重新打开它,一切都会恢复正常,即我无法“单击”UC2 中的任何控件。

I have a usercontrol (UC1) that changes aspect at design time according to what the user wants to show.

  • A regular button that pops a window with usercontrol UC2 (the window is only shown at runtime)
  • The UC2 directly hosted in UC1 (the regular button is then not shown)

Since I want to use the same UC2 instance in both situation, I just transfer ownership between UC1 and the form.

public UC1 ()
{
    _uc2 = new UC2 ();
}

public bool DisplayModeSimple
{
    get { return _displayModeSimple; }
    set
    {
        _displayModeSimple = value;
        if (_displayModeSimple)
        {
            // ... Verify if _uc2 is already in Controls...
            Controls.Remove (_uc2);
            uiButton.Visible = true;
        }
        else
        {
            // ... Verify that _uc2 is not in Controls ...
            Controls.Add (_uc2);
            uiButton.Visible = false;
        }
    }
}

private void HandleButtonClick (object sender, EventArgs e)
{
    // Not called if DisplayModeSimple=false since button is hidden...
    using (var form = new PopupForm (_uc2))
    {
        form.ShowDialog (this);
    }
}

Works fine in both design and runtime mode.

In design mode if I change the display mode UC1 behaves correctly.

However, controls that are on UC2 can be clicked like if it was runtime.
If I then close the form hosting UC1 and reopen it everything is back to normal, i.e., I cannot "click" on any controls in UC2.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

摇划花蜜的午后 2024-11-10 14:28:00

问题是您的第一个 UserControl 托管在 VS 上,因此它知道处于设计模式。第二个 UserControl 托管在第一个 UserControl 中,因此由于其宿主不是 Designer,因此它认为位于普通容器中并具有相应的行为。如何解决这个问题有点棘手,因为据我所知,没有简单的解决方案。 在这里您可以找到一些解决方法。另一种可能是递归地测试 Site.DesignMode,但这取决于控件的深度级别。

The problem is that your first UserControl is hosted on VS, so it knows to be in design mode. The second UserControl is hosted in the first UserControl, so as its host is not a Designer, it thinks to be in a normal container and behaves accordingly. How to solve that is a bit tricky, as there isn't asimple solution AFAIK. Here you can find some workarounds. Another could be to test Site.DesignMode recursively, but it depends on the level of depth of your controls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文