获取列表框以使用窗口调整大小,但不使用内容调整大小 [英] Get ListBox to resize with window, but not resize with content

查看:56
本文介绍了获取列表框以使用窗口调整大小,但不使用内容调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

必须对此问题有一个优雅的解决方案,但我在网上找不到任何东西.我有一个具有一列和一列的宽度/高度*的网格,其中包含一个ListBox.我将Window的SizeToContents设置为WidthAndHeight,以允许将窗口的大小调整为每组UI窗口小部件/字体的适当大小.当我将项目添加到ListBox时,它会调整大小,导致窗口增大.

There must be an elegant solution to this problem, but I can't find anything online. I have a grid that has one column and row with width/height *, containing a ListBox. I have my Window SizeToContents set to WidthAndHeight to allow the window to resize to the proper size for each set of UI widgets/fonts. When I add items to the ListBox, it resizes, causing the window to grow.

如果要更改窗口的大小,我希望ListBox能够调整大小,但是如果添加的内容比ListBox的宽度长,我希望滚动条出现而不是使其增大,从而导致Window变大生长.如果我为Window设置了显式的大小并将SizeToContent设置为Manual(默认值),它将按我的预期工作.

I want the ListBox to resize if I change the size of the window, but if I add content that is longer than the width of the ListBox, I want the scrollbar to appear and not for it to grow, causing the Window to grow. If I set explicit sizes for the Window and set SizeToContent to Manual (the default), it works as I intend.

是否有任何方法可以在启动时将窗口的大小调整为内容的大小,并继续使ListBox随窗口的大小而增加,但不随其内容而增加?

Is there any way to size the window to the contents at startup and continue to have the ListBox grow with the window size, but not with its content?

推荐答案

按照Potecaru Tudor的建议,我通过将ListBox包裹在另一个容器中并将ListBox的Height绑定到容器的ActualHeight来解决了类似的问题.

Following Potecaru Tudor's suggestion, I solved a similar issue by wrapping the ListBox in another container and binding the ListBox's Height to the ActualHeight of the container.

这是一个XAML代码段,可帮助您:

Here is a XAML code snippet to help:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Border x:Name="HeightHelperPanel" Grid.Row="0">
        <ListBox ItemsSource="{Binding Path=FileNameCollection}"
                 MinHeight="60"
                 Height="{Binding Path=ActualHeight, ElementName=HeightHelperPanel}"/>
    </Border>
</Grid>

基本上,边框容器在其内容增长时不会增长,但会一直延伸到网格行的高度.网格行将填充包含窗口允许的任何空间,但不想在窗口上施加任何高度限制.因此,ListBox的高度被限制为Border的高度.

Basically, the Border container doesn't grow when its content grows, but it will stay stretched to the height of the grid row. The grid row will fill up any space the containing window will allow it, but will not want to force any height constraints on the window. The ListBox's height is therefore constrained to the height of the Border.

希望对遇到这个令人沮丧的问题的其他人有所帮助.

Hope that helps anyone else who has had this somewhat frustrating issue.

这篇关于获取列表框以使用窗口调整大小,但不使用内容调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆