使用滚动条自动调整wpf元素大小(富文本框、列表框)vb

发布于 2024-09-18 12:45:11 字数 186 浏览 6 评论 0原文

我遇到一个问题,我有列表框和富文本框等元素,我想根据窗口的大小在 xaml 中自动设置其大小,但我只想将其调整为窗口的大小,然后如果内容大于此值,请放置滚动条。

不幸的是,我可以让滚动条工作的唯一方法是设置列表框/富文本框的特定高度(这不起作用,因为我希望它自动调整大小到它所包含的网格的高度, 是窗口的高度(自动)。

这通常

I'm having a problem where I have elements such as Listboxes and Rich Text boxes that I want to set to size automatically in xaml according to the size of the window, but I only want it to resize to the size of the window and then put scrollbars if the content is any bigger than that.

Unfortunately, the only way I can get scroll bars to work is if I set a specific height of the listbox/rich text box (which does not work because I want it to automatically resize to the height of the grid that it is contained within, which is generally the height of the window (auto).

Any help would be greatly appreciated.

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

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

发布评论

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

评论(1

与之呼应 2024-09-25 12:45:11

您不需要为 WidthHeight 使用固定值 - 您应该使用 MinWidthMinHeight 属性。然后尝试与此类似的布局:

<Window>
    <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
        <Grid>

            <ListBox MinWidth="500" MinHeight="250"/>

            <!-- any other controls... -->

        </Grid>
    </ScrollViewer>
</Window>

如果将对齐属性设置为 Stretch 并且至少将一行/列设置为,则 Grid 通常会使用它获得的所有空间星星大小。在这种情况下,只有一行和一列,隐式创建,默认都是星形大小。
要使 ScrollViewer 工作,您需要以某种方式设置内容控件的最小大小,否则 ScrollViewer知道何时激活ScrollBar。在上面的示例中,我使用 ListBoxMinHeightMinWidth 属性来完成此操作,但您也可以在 ListBox 上设置这些属性。 code>Grid 的 RowDefinition 和/或 ColumnDefinition

现在,如果您调整窗口大小,使 Width 变得小于 500,您将看到滚动条出现。看看吧。

You do not need to use fixed values for Width and Height - you should rather specify a minimum width/height for your controls using the MinWidth and MinHeight properties. Then try a layout similar to this:

<Window>
    <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
        <Grid>

            <ListBox MinWidth="500" MinHeight="250"/>

            <!-- any other controls... -->

        </Grid>
    </ScrollViewer>
</Window>

The Grid generally uses all the space it gets if its alignment properties are set to Stretch and if at least one row/column is set to be star-sized. In this case, there are only one row and one column, implicitly created, both star-sized by default.
To make the ScrollViewer work, you need to somehow set a minimum size of your content controls because otherwise the ScrollViewer does not know when to activate the ScrollBars. In the example above, I have done that using the MinHeight and MinWidth properties of the ListBox, but you could also set these properties on the Grid's RowDefinitions and/or ColumnDefinitions.

Now, if you resize the window, so that the Width becomes smaller than 500, you will see that scrollbars will appear. Just check it out.

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