How to add a ScrollBar to a Stackpanel

后端 未结 5 1036
刺人心
刺人心 2020-12-08 18:12

In my WPF application, I have a Stackpanel containing several controls inside them. How can I add a Scrollbar to this stackpanel.

相关标签:
5条回答
  • 2020-12-08 18:37

    Stackpanel doesn't have built in scrolling mechanism but you can always wrap the StackPanel in a ScrollViewer

    <ScrollViewer VerticalScrollBarVisibility="Auto">
      <StackPanel ... />
    </ScrollViewer>
    
    0 讨论(0)
  • 2020-12-08 18:39

    For horizontally oriented StackPanel, explicitly putting both the scrollbar visibilities worked for me to get the horizontal scrollbar.

        <ScrollViewer VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Auto" >
            <StackPanel Orientation="Horizontal" />
        </ScrollViewer>
    
    0 讨论(0)
  • 2020-12-08 18:42

    It works like this:

    <ScrollViewer VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Disabled" Width="340" HorizontalAlignment="Left" Margin="12,0,0,0">
            <StackPanel Name="stackPanel1" Width="311">
    
            </StackPanel>
    </ScrollViewer>
    

    TextBox tb = new TextBox();
    tb.TextChanged += new TextChangedEventHandler(TextBox_TextChanged);
    stackPanel1.Children.Add(tb);
    
    0 讨论(0)
  • 2020-12-08 18:48

    Put it into a ScrollViewer.

    0 讨论(0)
  • 2020-12-08 18:52

    If you mean, you want to scroll through multiple items in your stackpanel, try putting a grid around it. By definition, a stackpanel has infinite length.

    So try something like this:

       <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <StackPanel Width="311">
                  <TextBlock Text="{Binding A}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" FontStretch="Condensed" FontSize="28" />
                  <TextBlock Text="{Binding B}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
            </StackPanel>
        </Grid>
    

    You could even make this work with a ScrollViewer

    0 讨论(0)
提交回复
热议问题