如何让ScrollViewer在StackPanel内部工作?

Edw*_*uay 70 wpf xaml scrollviewer stackpanel

在下面的WPF XAML中,ScrollViewer不起作用(它显示滚动条但不能滚动,内容从窗口向下移动到底部).

我可以将外部StackPanel更改为Grid,它将起作用.

但是,在我复制以下代码的应用程序中,我需要一个外部StackPanel.我需要对StackPanel做什么才能让ScrollViewer显示一个可用的滚动条?例如VerticalAlignment ="Stretch"Height ="Auto"不起作用.

 <StackPanel>
        <ScrollViewer>
            <StackPanel>
                <TextBlock Text="This is a test"/>
                <TextBlock Text="This is a test"/>
                <TextBlock Text="This is a test"/>
                <TextBlock Text="This is a test"/>
                <TextBlock Text="This is a test"/>
                <TextBlock Text="This is a test"/>
                <TextBlock Text="This is a test"/>
                <TextBlock Text="This is a test"/>
                <TextBlock Text="This is a test"/>
                <TextBlock Text="This is a test"/>
                <TextBlock Text="This is a test"/>
                <TextBlock Text="This is a test"/>
                <TextBlock Text="This is a test"/>
                <TextBlock Text="This is a test"/>
                <TextBlock Text="This is a test"/>
                <TextBlock Text="This is a test"/>
                <TextBlock Text="This is a test"/>
                <TextBlock Text="This is a test"/>
                <TextBlock Text="This is a test"/>
                <TextBlock Text="This is a test"/>
                <TextBlock Text="This is a test"/>
                <TextBlock Text="This is a test"/>
                <TextBlock Text="This is a test"/>
                <TextBlock Text="This is a test"/>
                <TextBlock Text="This is a test"/>
                <TextBlock Text="This is a test"/>
                <TextBlock Text="This is a test"/>
            </StackPanel>
        </ScrollViewer>
 </StackPanel>
Run Code Online (Sandbox Code Playgroud)

Ken*_*art 57

你不能没有固定的高度StackPanel.它旨在无限制地向一个方向发展.我建议使用不同的Panel.为什么你"需要"拥有外表StackPanel

  • 想要堆叠东西并使用网格你必须手动管理所有行和列,但DockPanel工作得很好所以我会切换到那个,谢谢. (13认同)
  • 我应该在UW​​P中使用哪种替代控制?没有DockPanel.谢谢. (2认同)
  • 该死的stackpanel,我总是要用UWP上的网格替换它们,它们应该改变它的行为它是唯一可以这样工作的面板 (2认同)

Rob*_*Rob 56

这也困扰了我一段时间,诀窍是将你的stackpanel放在一个scrollviewer中.

此外,您需要确保将滚动查看器的CanContentScroll属性设置为True,这是一个示例:

  <ScrollViewer Grid.Row="1" Margin="299,12,34,54" Name="ScrollViewer1" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Height="195" CanContentScroll="True">
        <StackPanel Name="StackPanel1" OverridesDefaultStyle="False"  Height="193" Width="376" VerticalAlignment="Top" HorizontalAlignment="Left"></StackPanel>
  </ScrollViewer>
Run Code Online (Sandbox Code Playgroud)

  • Giddy>请看这个链接: - http://msdn.microsoft.com/en-us/library/ms612683.aspx (2认同)
  • “您需要确保将滚动查看器的 CanContentScroll 属性设置为 True”——我仍然无法相信这不是名为“ScrollViewer”的控件的默认设置。 (2认同)

T.J*_*aer 8

请注意,有时您可能没有实现StackPanel.就我而言,我有这个代码

<ScrollViewer>
  <ItemsControl ItemsSource="{Binding Pages}"/>
</ScrollViewer>
Run Code Online (Sandbox Code Playgroud)

工作得很好.绑定引用的"Pages"实际上是不同的,复杂的UserControls,我想在其中一些上只有滚动条.所以我删除了scrollviewer:

 <ItemsControl ItemsSource="{Binding Pages}"/>
Run Code Online (Sandbox Code Playgroud)

然后我将ScrollViewer作为我想要的用户控件的顶级元素.但是,这不起作用.内容刚刚流出页面.起初我不认为这个问题/答案可以帮助我,但我意识到ItemsControl的默认ItemPanel是StackPanel.所以我通过指定一个不是StackPanel的ItemsPanel解决了我的问题:

<ItemsControl ItemsSource="{Binding Pages}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)


Lui*_*ado 5

事实上,我解决这个问题的方法是删除外部堆栈面板,并将滚动查看器设置在主网格内我想要的位置。

        <Grid Style="{StaticResource LayoutRootStyle}">
    <Grid.RowDefinitions>
        <RowDefinition Height="160"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>        

    <!-- Vertical scrolling grid used in most view states -->    

        <ScrollViewer Grid.Row="1" HorizontalScrollBarVisibility="Auto">
            <StackPanel Orientation="Horizontal">
                <GridView>
                ...
                </GridView>
            </StackPanel>
        </ScrollViewer>        
Run Code Online (Sandbox Code Playgroud)


Kyl*_*Ren 5

这是它的工作原理:

<Window x:Class="TabControl.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
    xmlns:local="clr-namespace:TabControl"
    Title="MainWindow"    Height="300"   
    DataContext="{Binding RelativeSource={RelativeSource Self}}"         
    >    
<StackPanel>
    <ScrollViewer Height="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Border}},Path=ActualHeight}" >
        <StackPanel >
            <TextBlock Text="This is a test"/>                <TextBlock Text="This is a test"/>
            <TextBlock Text="This is a test"/>                <TextBlock Text="This is a test"/>
            <TextBlock Text="This is a test"/>                <TextBlock Text="This is a test"/>
            <TextBlock Text="This is a test"/>                <TextBlock Text="This is a test"/>
            <TextBlock Text="This is a test"/>                <TextBlock Text="This is a test"/>
            <TextBlock Text="This is a test"/>                <TextBlock Text="This is a test"/>
            <TextBlock Text="This is a test"/>                <TextBlock Text="This is a test"/>
            <TextBlock Text="This is a test"/>                <TextBlock Text="This is a test"/>
            <TextBlock Text="This is a test"/>                <TextBlock Text="This is a test"/>
            <TextBlock Text="This is a test"/>                <TextBlock Text="This is a test"/>
            <TextBlock Text="This is a test"/>                <TextBlock Text="This is a test"/>
            <TextBlock Text="This is a test"/>                <TextBlock Text="This is a test"/>
            <TextBlock Text="This is a test"/>                <TextBlock Text="This is a test"/>
            <TextBlock Text="This is a test"/>                <TextBlock Text="This is a test"/>
            <TextBlock Text="This is a test"/>                <TextBlock Text="This is a test"/>
            <TextBlock Text="This is a test"/>                <TextBlock Text="This is a test"/>
            <TextBlock Text="This is a test"/>                <TextBlock Text="This is a test"/>
            <TextBlock Text="This is a test"/>                <TextBlock Text="This is a test"/>
            <TextBlock Text="This is a test"/>                <TextBlock Text="This is a test"/>
            <TextBlock Text="This is a test"/>                <TextBlock Text="This is a test"/>
            <TextBlock Text="This is a test"/>                <TextBlock Text="This is a test"/>
            <TextBlock Text="This is a test"/>                <TextBlock Text="This is a test"/>
            <TextBlock Text="This is a test"/>                <TextBlock Text="This is a test"/>
            <TextBlock Text="This is a test"/>                <TextBlock Text="This is a test"/>
            <TextBlock Text="This is a test"/>                <TextBlock Text="This is a test"/>
            <TextBlock Text="This is a test"/>                <TextBlock Text="This is a test"/>
            <TextBlock Text="This is a test"/>                <TextBlock Text="This is a test"/>
            <TextBlock Text="This is a test"/>                <TextBlock Text="This is a test"/>
        </StackPanel>
    </ScrollViewer>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

通过将 ScrollViewer 的高度绑定到窗口的内部高度。

调整大小的逻辑是我们需要给任何元素固定高度或设计视图以使用渲染高度。

输出:

Stackpanel 中的滚动条