在堆栈面板中,我在运行时从代码后面添加了一些标签:我想使堆栈面板可滚动。在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>
在后面的代码中,我向堆栈面板添加了一些标签:
for(.....)
{
stackPanelVistaProfiloTessera.Children.Add(new Label {....});
}
为什么堆栈面板无法滚动?我该如何解决?
谢谢
最佳答案
从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>
输出看起来像这样。
如果您将高度和宽度用于设计目的,则可以将边距设置为stackpanel。
关于c# - WPF如何使StackPanel可滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23168565/