如何设置GroupBox高度来调整窗口大小,如果内容更大,则需要显示ScrollViewer

时间:2015-04-12 04:19:53

标签: .net wpf xaml

我希望GroupBox高度可以调整窗口大小,如果内容的高度较大,则显示ScrollViewer。但它将大小设置为大小内容或MaxHeight(如果设置)。

<StackPanel>
  <GroupBox>
    <ScrollViewer VerticalAlignment="Top" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Visible">
      <StackPanel>
        <Button VerticalAlignment="Top">Button</Button>
        <Button VerticalAlignment="Top">Button</Button>
        <Button VerticalAlignment="Top">Button</Button>
        <Button VerticalAlignment="Top">Button</Button>
        <Button VerticalAlignment="Top">Button</Button>
        <Button VerticalAlignment="Top">Button</Button>
        <Button VerticalAlignment="Top">Button</Button>
        <Button VerticalAlignment="Top">Button</Button>
        <Button VerticalAlignment="Top">Button</Button>
        <Button VerticalAlignment="Top">Button</Button>
        <Button VerticalAlignment="Top">Button</Button>
        <Button VerticalAlignment="Top">Button</Button>
        <Button VerticalAlignment="Top">Button</Button>
        <Button VerticalAlignment="Top">Button</Button>
        <Button VerticalAlignment="Top">Button</Button>
        <Button VerticalAlignment="Top">Button</Button>
        <Button VerticalAlignment="Top">Button</Button>
        <Button VerticalAlignment="Top">Button</Button>
        <Button VerticalAlignment="Top">Button</Button>
        <Button VerticalAlignment="Top">Button</Button>
      </StackPanel>
    </ScrollViewer>
  </GroupBox>
</StackPanel>

1 个答案:

答案 0 :(得分:0)

我发现如果将外部StackPanel替换为DockPanel它将起作用。

 <DockPanel>
  <GroupBox DockPanel.Dock="Top">
    <ScrollViewer VerticalAlignment="Top" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Visible">
      <StackPanel>
        <Button VerticalAlignment="Top">Button</Button>