简体   繁体   English

WPF如何使StackPanel可滚动

[英]WPF how make StackPanel Scrollable

in a stackpanel i add some labels from code behind at runtime: i want make the stackpanel scrollable. 在stackpanel中,我在运行时从代码中添加一些标签:我想让stackpanel可滚动。 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: 在后面的代码我添加一些标签到stackpanel:

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

Why the stackpanel isn't scrollable? 为什么stackpanel不可滚动? How can i resolve this? 我该如何解决这个问题?

Thanks 谢谢

Remove height and width from stackpanel..rest is working fine here. 从stackpanel删除高度和宽度..这里工作正常。

<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。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM