stackpanel中的垂直滚动

jos*_*003 13 wpf

我想让一个滚动条放在堆栈面板上.滚动条显示但不允许用户移动滚动条.我的XMAL有问题还是有更多的东西?

<GroupBox HorizontalAlignment="Left" Margin="268,8,0,0" VerticalAlignment="Top" Width="505.881" Height="352.653" Header="Metrics">
<Grid>
    <ScrollViewer>
        <StackPanel>
              </StackPanel>
          </ScrollViewer>
      </Grid>
</GroupBox>
Run Code Online (Sandbox Code Playgroud)

堆栈面板的内容是扩展器,其中包含数据.

ASa*_*nch 22

您不能设置GroupBox的宽度和高度,以使内部ScrollViewer工作.试试这个,你会发现它会正常工作.

<GroupBox Header="Metrics" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="268,8,0,0">
    <Grid>
        <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
            <StackPanel>
                <Expander Header="Expander">
                    <StackPanel>
                        <Button>Test</Button>
                        <Button>Test</Button>
                        <Button>Test</Button>
                        <Button>Test</Button>
                        <Button>Test</Button>
                        <Button>Test</Button>
                        <Button>Test</Button>
                        <Button>Test</Button>
                        <Button>Test</Button>
                    </StackPanel>
                </Expander>

            </StackPanel>
        </ScrollViewer>
    </Grid>
</GroupBox>
Run Code Online (Sandbox Code Playgroud)

  • 可以在ScrollViewer中设置项目的宽度和高度.所以在上面的示例代码中,这意味着可以设置内部StackPanel的宽度或高度.如果您有<StackPanel Width ="200"Height ="200",这意味着只要ScrollViewer的宽度和/或高度小于200,就会启用滚动. (3认同)