![](/img/trans.png)
[英]WPF, making scrollviewer work with stackpanel programatically
[英]StackPanel in ScrollViewer WPF
我在ScrollViewer的ScrollBar的极端奇怪行为方面遇到了问题。
这是我的代码:
<ScrollViewer CanContentScroll="True">
<StackPanel>
<StackPanel>
<Button Height="40"/>
<Button Height="40"/>
<Button Height="40"/>
<Button Height="40"/>
<Button Height="40"/>
<Button Height="40"/>
<Button Height="40"/>
<Button Height="40" Background="Yellow"/>
</StackPanel>
<StackPanel>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Green"/>
</StackPanel>
</StackPanel>
</ScrollViewer>
问题出在StackPanel内的那些StackPanel中。 如果其中只有一个主StackPanel,但内部没有StackPanel,则正常运行。
我已经尝试过在主StackPanel内部使用ScrollViewer作为每个孩子的孩子。 该解决方案中的另一个问题是我不想将StackPanels的高度固定。
@EDIT:问题在于ScrollBar不能平稳移动,并且它阻止显示所有内容。 抱歉,缺少信息。
因为在ScrollViewer
CanContentScroll
设置为True,所以您会得到这种奇怪的行为。 这意味着,ScorllViewer将每个StackPanel
视为单个内容元素,并按每个StackPanel
的高度而不是子StackPanel
内每个Button
的高度滚动。
要摆脱这种奇怪的行为,请将您的代码更改为:
<ScrollViewer>
<StackPanel>
<StackPanel>
<Button Height="40"/>
<Button Height="40"/>
<Button Height="40"/>
<Button Height="40"/>
<Button Height="40"/>
<Button Height="40"/>
<Button Height="40"/>
<Button Height="40" Background="Yellow"/>
</StackPanel>
<StackPanel>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Green"/>
</StackPanel>
</StackPanel>
</ScrollViewer>
您“需要”将您的StackPanels添加到ItemsControl集合中:
<ScrollViewer CanContentScroll="True">
<ItemsControl >
<StackPanel>
<StackPanel>
<Button Height="40"/>
<Button Height="40"/>
<Button Height="40"/>
<Button Height="40"/>
<Button Height="40"/>
<Button Height="40"/>
<Button Height="40"/>
<Button Height="40" Background="Yellow"/>
</StackPanel>
<StackPanel>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Red"/>
<Button Height="40" Background="Green"/>
</StackPanel>
</StackPanel>
</ItemsControl >
</ScrollViewer>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.