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.
and you can set margin to stackpanel if you are using height and width for design purpose.
更多推荐
stackpanel,stackPanelVistaProfiloTessera,面板,Height,电脑培训,计算机培训,IT培训"/>
发布评论