WPF如何使StackPanel Scrollable(WPF how make StackPanel Scrollable)

编程入门 行业动态 更新时间:2024-10-22 17:33:01
WPF如何使StackPanel Scrollable(WPF how make StackPanel Scrollable)

在一个堆栈面板中,我在运行时从代码中添加一些标签:我想让堆栈面板可以滚动。 在xaml文件中我有:

<ScrollViewer HorizontalAlignment="Left" Height="299" Margin="592,120,0,0" VerticalAlignment="Top" Width="188" VerticalScrollBarVisibility="Auto"> <StackPanel x:Name="stackPanelVistaProfiloTessera" Height="292" Width="170"/> </ScrollViewer>

在后面的代码中,我添加了一些标签到stackpanel:

for(.....) { stackPanelVistaProfiloTessera.Children.Add(new Label {....}); }

为什么堆栈面板不可滚动? 我该如何解决这个问题?

谢谢

in a stackpanel i add some labels from code behind at runtime: i want make the stackpanel scrollable. In the xaml file i have:

<ScrollViewer HorizontalAlignment="Left" Height="299" Margin="592,120,0,0" VerticalAlignment="Top" Width="188" VerticalScrollBarVisibility="Auto"> <StackPanel x:Name="stackPanelVistaProfiloTessera" Height="292" Width="170"/> </ScrollViewer>

In the code behind i add some label to the stackpanel:

for(.....) { stackPanelVistaProfiloTessera.Children.Add(new Label {....}); }

Why the stackpanel isn't scrollable? How can i resolve this?

Thanks

最满意答案

从stackpanel..rest移除高度和宽度在这里工作正常。

<ScrollViewer HorizontalAlignment="Left" Background="Green" Height="299" Margin="592,120,0,0" VerticalAlignment="Top" Width="188" VerticalScrollBarVisibility="Auto"> <StackPanel x:Name="stackPanelVistaProfiloTessera" Background="RoyalBlue" > <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> </StackPanel> </ScrollViewer>

输出看起来像这样。

在这里输入图像描述

如果您使用高度和宽度进行设计,您可以将边距设置为堆叠面板。

Remove height and width from stackpanel..rest is working fine here.

<ScrollViewer HorizontalAlignment="Left" Background="Green" Height="299" Margin="592,120,0,0" VerticalAlignment="Top" Width="188" VerticalScrollBarVisibility="Auto"> <StackPanel x:Name="stackPanelVistaProfiloTessera" Background="RoyalBlue" > <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> <Label Height="30" Width="100" Margin="5">label1</Label> </StackPanel> </ScrollViewer>

Output look like this.

enter image description here

and you can set margin to stackpanel if you are using height and width for design purpose.

更多推荐

stackpanel,stackPanelVistaProfiloTessera,面板,Height,电脑培训,计算机培训,IT培训"/>

本文发布于:2023-08-06 23:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1457543.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:WPF   StackPanel   Scrollable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!