ListBox 包含在 ScrollViewer 中
我希望当所有信息无法在屏幕上显示时显示页面的ScrollViewer(即调整窗口大小) 但是,这里的列表框不会滚动,并且会一直绘制到页面底部,除非我将其设置为具有 MaxSize。有没有一种方法可以优先让 ListBox 在我制作的 ScrollViewer 之前显示它的 ScrollViewer ?
我现在拥有的 https://i.sstatic.net/O4h7u.png
我想实现什么,但是我在 此处使用了 ListBox 的 MaxHeight。
这是我的一些标记:
<ScrollViewer HorizontalAlignment="Stretch" VerticalScrollBarVisibility="Auto" Name="scrollViewer1" VerticalAlignment="Stretch" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ComboBox Grid.Row="0" Width="120" HorizontalAlignment="Left"></ComboBox>
<ListBox HorizontalAlignment="Left" Name="listBox" Width="120" Grid.Row="1" <!--MaxHeight="500"--> />
</Grid>
</ScrollViewer>
I would like the ScrollViewer of the page to be displayed when all the information cannot be shown on the screen (i.e. resize the window)
However, the ListBox here doesn't get a scroll and it gets sketch till the bottom of the page unless i set it to have a MaxSize. Is there a way to give priority to the ListBox to display its ScrollViewer before the one I have made?
what i have right now
https://i.sstatic.net/O4h7u.png
what i would like to achieve, but i used a MaxHeight for the ListBox here.
Here's some my markup:
<ScrollViewer HorizontalAlignment="Stretch" VerticalScrollBarVisibility="Auto" Name="scrollViewer1" VerticalAlignment="Stretch" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ComboBox Grid.Row="0" Width="120" HorizontalAlignment="Left"></ComboBox>
<ListBox HorizontalAlignment="Left" Name="listBox" Width="120" Grid.Row="1" <!--MaxHeight="500"--> />
</Grid>
</ScrollViewer>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我知道这个问题已经很老了,但我遇到了完全相同的问题,并想出了一些解决方案,但这比问题未解决要好。
我读过的大部分内容都指出,在这种情况下使用像 StackPanel 这样的东西是不好的,因为面板会增长以适应它所容纳的元素。通常这工作正常,因为您可以将 StackPanel 粘贴到 Grid 中并设置列/行的 MinHeight 和 MaxHeight 并将控件锁定到位。一旦添加了 ScrollView,这种情况就会变得很糟糕。上面的答案很好地描述了问题,但缺乏解决方案。
我尝试了许多不同类型的面板而不是 StackPanel,但它们都产生相同的结果。我决定,由于我的 ListBox 位于 Grid 内部,因此我需要将该网格位置的 MaxHeight 绑定到控件中的某个其他值,以防止 ListBox 增长。这样做的问题是,没有任何元素可以直接绑定到并获得您想要的确切高度。
输入技巧:
我的高度有点太大,创建了一个奇怪的总是在屏幕外的列表框(实际上 36 像素太大,这是列表框上方标签的高度)。所以我实现了 IValueConverter:
}
之后我所做的就是将其作为 MaxHeight 绑定的转换器(注意您需要命名您的用户控件并绑定到它的 x:Name):
我能想到的唯一其他替代方案是扩展其中一个面板并尝试发挥其生长行为。我承认这是一个黑客行为,但它会起作用。
I know this question is old, but I had exactly the same problem and came up with a bit of a hack as a fix but its better than having the question unsolved.
Much of what I've read states that using things like StackPanel is bad in this case because the panel grows to fit the elements it holds. Normally this works fine because you can stick the StackPanel into a Grid and set the MinHeight and MaxHeight of the column/row and lock the controls in place. As soon as the ScrollView is added this kind of goes to hell. The answer above describes the problem well, but lacks a solution.
I tried many different types of Panels instead of a StackPanel but they all yield the same result. I decided that since my ListBox sits inside of a Grid, I needed to bind the MaxHeight of that grid location to some other value in the control to keep the ListBox from growing. The problem with this is that there is no element that you can bind straight to and get the exact height your looking for.
Enter the hack:
My height was just a tiny bit too big creating a weird always offscreen ListBox (in fact 36 pixels too large, which was the height of the label above the ListBox). So I implemented the IValueConverter:
}
All I did after that was include it as a converter for the binding on MaxHeight (Note you need to name your usercontrol and bind to its x:Name):
The only other alternative I can think of is to extend one of the panels and try to play with its growth behavior. I admit this is a hack, but it will work.
试试这个
或者你也可以试试这个
Try this
Or you can also try this
您的定义存在逻辑不一致。
正如您所说的要求:“当所有信息无法显示在屏幕上时,我希望显示页面的滚动查看器[不使用MaxHeight]” - 出现了一个问题:“您如何确定'所有信息无法在屏幕上显示'?”或“
ListBox
在什么时候应该停止增长并显示滚动条?”。从 WPF\Silverlight 布局管理逻辑中,它完全符合您的要求 - 当列表框的高度加上组合框的高度之和大于滚动查看器的
ViewportHeight
时 - 您会得到滚动条。只有当您允许ListBox
在没有滚动条的情况下增长到所需的大小时,才有可能实现这一点。You have a logical inconsistency in your definitions.
The requirement as you put it: "I would like the scrollviewer of the page to be displayed when all the information cannot be shown on the screen [without using MaxHeight]" - a question arises: "How do you determine that 'all the information cannot be shown on the screen'?" or "At what point the
ListBox
should stop growing and show the scroll bar?".From a WPF\Silverlight layout management logic, it does exactly what you want - when the sum of height of list box plus the height of the combo box is greater than the
ViewportHeight
of the scroll viewer - you get a scroll bar. That is possible only when you allow theListBox
to grow to it's desired size without scroll bars.不要在
ListBox
上设置 MaxHeight,只需在RowDefinitions
中使用星号“*”符号即可使 2 个控件之间的相对大小保持正确。Dont set the MaxHeight on the
ListBox
, just use the star '*' notation in yourRowDefinitions
to get the relative sizing between your 2 controls correct.另一个解决方案:
Another solution: