WPF how make StackPanel Scrollable

和自甴很熟 提交于 2019-12-23 15:01:18

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/23168565/wpf-how-make-stackpanel-scrollable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!