将 WPF 控件设置为扩展以填充可用空间,仅此而已
如何设置 WPF 控件来填充其父级容器中的可用空间,但不展开父级?
以下代码片段描述了我正在尝试的布局。我希望 Grid
能够拉伸以容纳 Expander
,并且我希望 ListBox
仅填充 Grid
>。我希望当 Grid
太小而无法显示所有 ListBoxItem
时,显示 ListBox
的滚动条。
<ScrollViewer>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<ListBox Grid.Row="0" Grid.Column="0" />
<Expander Grid.Row="0" Grid.Column="1" Header="Expander" />
</Grid>
</ScrollViewer>
目前发生的情况是 Grid
拉伸以适合整个 ListBox
,并且出现外部 ScrollViewer
的垂直滚动条。我只希望当 Expander
太大而无法在屏幕上显示时显示外部滚动条。
How can I set up a WPF control to fill the available space in its parent's container, but not expand the parent?
The following snippet describes the layout I am attempting. I would like the Grid
to stretch to accommodate the Expander
, and I would like the ListBox
only to fill the Grid
. I want the ListBox
's scroll bar to appear when the Grid
is too small to show all the ListBoxItem
s.
<ScrollViewer>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<ListBox Grid.Row="0" Grid.Column="0" />
<Expander Grid.Row="0" Grid.Column="1" Header="Expander" />
</Grid>
</ScrollViewer>
Currently what happens is that the Grid
stretches to fit the entire ListBox
, and the outer ScrollViewer
's vertical scroll bar appears. I only want the outer scroll bar to appear when the Expander
gets too big to fit on screen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了解决同样的问题,我编写了特殊的容器类:
用容器包围你的ListBox,ListBox的高度将与Expander的高度相同。
请注意,我从列的定义中删除了
Width="Auto"
,因为 FrugalContainer 将尽可能小。所以你不能将父网格单元格的宽度或高度设置为自动。如果需要自动调整大小,请重写容器:
To solve the same problem I wrote special container class:
Surround your ListBox by the container and the height of ListBox will be the same as of Expander.
Note that I remove
Width="Auto"
from column's definition because the FrugalContainer will be as small as it can. So you can't set the width or height of the parent grid's cell to Auto.If you need autosizing, rewrite the container:
ScrollViewer
有什么意义?当可用空间太小时,让ListBox
模板中的ScrollViewer
自然出现即可。What is the point of the
ScrollViewer
? Just let theScrollViewer
in theListBox
template appear naturally when too little room is available.