用户控件内的 WPF 滚动查看器不显示垂直滚动条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20889017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Scrollviewer inside usercontrol doesn't show vertical scrollbar
提问by user2330678
I am using PRISM architecture.
我正在使用 PRISM 架构。
I have a structure like below
我有一个像下面这样的结构
<Window Height="300" Width="300">
<Grid>
<ItemsControl>
<UserControl>
<ScrollViewer>
<StackPanel>
</StackPanel>
</ScrollViewer>
</UserControl>
</ItemsControl>
</Grid>
</Window>
Horizontal scrollbar shows up but the vertical doesn't.
水平滚动条显示,但垂直不显示。
Please note that changing stackpanel to grid like below doesn't help.
请注意,将堆栈面板更改为如下所示的网格无济于事。
<UserControl>
<ScrollViewer>
<Grid>
<StackPanel>
</StackPanel>
</Grid>
</ScrollViewer>
</UserControl>
回答by Mat J
Place ScrollViewerinside a Grid. This helps the ScrollViewerto use the available space within Gridand if content overflows, it will show scrollbars. It depends on where you use your UserControlthough. Make sure this UserControlis not placed inside a ScrollVieweror any scrollable control.
广场ScrollViewer内Grid。这有助于ScrollViewer使用其中的可用空间Grid,如果内容溢出,它将显示滚动条。这取决于你在哪里使用你的UserControl虽然。确保它UserControl没有放置在 aScrollViewer或任何可滚动控件中。
<UserControl>
<Grid>
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<StackPanel>
</StackPanel>
</ScrollViewer>
</Grid>
</UserControl>
回答by Rohit Vats
StackPanelgrows on indefinitely (i.e. height is not restricted until you fix it be setting explicitly).
StackPanel无限增长(即高度不受限制,直到您明确设置它为止)。
Either use another panelsay Grid, DockPaneletc. or constrain fix height for your StackPanel.
要么使用另一个面板,比如Grid、DockPanel等,要么限制 StackPanel 的固定高度。
<ScrollViewer>
<DockPanel>
</DockPanel>
</ScrollViewer>
OR
或者
<ScrollViewer>
<StackPanel Height="300">
</StackPanel>
</ScrollViewer>
You can also constrain height like this:
您还可以像这样限制高度:
<StackPanel Height="{Binding ActualHeight,
RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}"/>

