嵌套流程布局面板未换行

发布于 2024-08-27 07:38:06 字数 1000 浏览 5 评论 0原文

我有一个 FlowLayoutPanel,其属性为:

  • Dock = Fill (在用户控件中)
  • FlowDirection = TopDown
  • WrapContents = false

我这样做,以便添加到面板的每个项目都添加到底部。

我添加到此面板的项目是用户控件,它们本身具有 FlowLayoutPanel,但它们具有标准行为(LeftToRight、WrapContents = true)。我遇到的问题是内部用户控件的 FlowLayoutPanel 没有调整大小以填充外部控件,但是当我在这些控件上将自动调整大小设置为 true 时,面板将不会包装其内容 - 这显然是一个已知问题。

如果它有助于形象化我正在尝试做的事情,它看起来像这样:

    ______________________________
    | __________________________ | Outer box = exterior flowlayout 
    | |Text____________________| |    (TopDown, NoWrap)
    | | # # # # # # # # # # # #| |
    | | # # # #                | | Interior boxes = usercontrols with text and a 
    | |________________________| |   flowlayoutpanel on them 
    | __________________________ |    (LeftToRight, Wrap)
    | |Text____________________| |   
    | | # # # # # # # # # # # #| |   # = pictures
    | | # #                    | |
    | |________________________| |
    |____________________________|

I've got a FlowLayoutPanel with properties:

  • Dock = Fill (in a usercontrol)
  • FlowDirection = TopDown
  • WrapContents = false

I do it this way so that each item added to the panel gets added to the bottom.

The items that I add to this panel are usercontrols which themselves have FlowLayoutPanels on them, however they have the standard behaviour (LeftToRight, WrapContents = true). The problem that I'm having is that the interior usercontrol's FlowLayoutPanel isn't resizing to fill the outer control, but when I set autosizing to true on these controls, then the panel won't wrap its contents - which is a known problem apparently.

If it helps visualize what I'm trying to do, it looks like this:

    ______________________________
    | __________________________ | Outer box = exterior flowlayout 
    | |Text____________________| |    (TopDown, NoWrap)
    | | # # # # # # # # # # # #| |
    | | # # # #                | | Interior boxes = usercontrols with text and a 
    | |________________________| |   flowlayoutpanel on them 
    | __________________________ |    (LeftToRight, Wrap)
    | |Text____________________| |   
    | | # # # # # # # # # # # #| |   # = pictures
    | | # #                    | |
    | |________________________| |
    |____________________________|

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

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

发布评论

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

评论(1

你的笑 2024-09-03 07:38:06

我认为您不能将控件停靠在 FlowLayoutPanel 中,除非您继承 LayoutEngine 并使用自定义引擎创建您自己的窗格版本。然而,这个问题有一个很棒的解决方案。使用 TableLayoutPanel!由于您只需要 1 列,因此使用 TableLayoutPanel 来实现此目的非常容易。

唯一需要注意的是,TLP 最初需要有 0 行,然后以编程方式添加用户控件。诀窍是将用户控件停靠到顶部。这是有效的:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        TableLayoutPanel tlp1 = new TableLayoutPanel();
        this.Controls.Add(tlp1);
        tlp1.Dock = DockStyle.Fill;

        for (int i = 0; i < 5; i++)
        {
            UserControl1 uc = new UserControl1();
            uc.Dock = DockStyle.Top;
            tlp1.Controls.Add(uc);
        }
    }
}

在本例中,UserControl1 是一个带有 FLP 的用户控件,其中有一堆按钮,因此我可以确认对接和流动是否有效。

I don't think you can dock controls in a FlowLayoutPanel, unless you subclass LayoutEngine and make your own version of the pane using your custom engine. However, there's an awesome solution to this problem. Use a TableLayoutPanel! Since you only want 1 column, it's very easy to use a TableLayoutPanel for this purpose.

The only caveat is that the TLP needs to have 0 rows initially, and you then add the user controls programmatically. And the trick is to dock the user control to Top. This works:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        TableLayoutPanel tlp1 = new TableLayoutPanel();
        this.Controls.Add(tlp1);
        tlp1.Dock = DockStyle.Fill;

        for (int i = 0; i < 5; i++)
        {
            UserControl1 uc = new UserControl1();
            uc.Dock = DockStyle.Top;
            tlp1.Controls.Add(uc);
        }
    }
}

UserControl1 in this case was a user control with a FLP on it which had a bunch of buttons in it so I could confirm that the docking and flowing would work.

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