如何使StackPanel内ScrollViewer的大小适应Window的大小?

时间:2015-10-25 19:45:04

标签: c# wpf visual-studio-2013 scrollviewer

我创建了一张尺寸为A4纸(21厘米x 29,7厘米)的固定尺寸帆布。

我想让它在ScrollViewer和StackPanel内滚动。 但是,似乎ScrollViewer的大小不适应窗口的大小。我怎样才能做到这一点?

到目前为止,这是我的代码。删除卷曲括号{}之间的文本是为了便于阅读。

<Window {xmlns stuff here} Width="900" Height="1200"  >
    <StackPanel>
        <my:Ribbon ShowQuickAccessToolBarOnTop="False">
            { ... Ribbon definition ... }
        </my:Ribbon>

        <ScrollViewer HorizontalScrollBarVisibility="Auto" 
                      VerticalScrollBarVisibility="Visible" > 

            <Canvas x:Name="PageCanvas"  HorizontalAlignment="Left"  
                    VerticalAlignment="Top" Width="21cm" Height="29.7cm"  >

                { ... Various drawings and elements here ... }

            </Canvas>
        </ScrollViewer>
    </StackPanel >
</Window>

1 个答案:

答案 0 :(得分:1)

StackPanel设计为在一个方向上无限增长。因此,您无需修复StackPanel的高度或无需将StackPanel替换为另一个Panel(例如Grid),就无法完成任务。

我建议您使用网格:

<Window {xmlns stuff here} Width="900" Height="1200"  >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto">
            <RowDefinition Height="*">
        </Grid.RowDefinitions>
        <my:Ribbon ShowQuickAccessToolBarOnTop="False" Grid.Row="0">
            { ... Ribbon definition ... }
        </my:Ribbon>

        <ScrollViewer HorizontalScrollBarVisibility="Auto"  Grid.Row="1"
                      VerticalScrollBarVisibility="Visible" > 

            <Canvas x:Name="PageCanvas"  HorizontalAlignment="Left"  
                    VerticalAlignment="Top" Width="21cm" Height="29.7cm"  >

                { ... Various drawings and elements here ... }

            </Canvas>
        </ScrollViewer>
    </StackPanel >
</Window>