2015-10-25 62 views
0

我創建了A4尺寸(21釐米x 29.7釐米)的固定尺寸帆布。如何調整StackPanel內的ScrollViewer的大小以適應Window的大小?

我想要這個在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

StackPanel被設計爲在一個方向無限增長。因此,如果不固定StackPanel的高度,或者沒有用另一個Panel(例如Grid)替換StackPanel,就無法完成任務。

我建議你使用網格:

<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> 
+0

好,謝謝! 'Height =「*」'中的星號(*)是什麼意思? –

+1

在這個具體情況下,它的意思是 - 「獲取所有剩餘空間」 – DmitryG

+1

查看[GridUnitType](https://msdn.microsoft.com/en-us/library/system.windows.gridunittype.aspx)更多細節 – DmitryG

相關問題