WPF 如何使 StackPanel 可滚动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23168565/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:34:53  来源:igfitidea点击:

WPF how make StackPanel Scrollable

c#.netwpfvisual-studio-2010stackpanel

提问by Tom

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

在堆栈面板中,我在运行时从后面的代码中添加了一些标签:我想让堆栈面板可滚动。在 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>

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

谢谢

回答by Heena Patil

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

从堆栈面板中删除高度和宽度..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>

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。