2010-04-27 29 views
1

我想建立一個WPF窗口,它使用外部網格屏幕分成4部分。在右下象限中,我想嵌入另一個大於網格單元格的網格。我一直在尋找添加ScrollViewer(或使用Grid.ScrollViewer屬性)的方法,但無論我嘗試什麼,內部網格不會調整大小或適當地顯示滾動條WPF/Silverlight中的可滾動網格

我懷疑它與有關,沒有用適當的尺寸(和調整大小)行爲包裝內部網格這將迫使內部網格兌現滾動條,而不是簡單地渲染太大(並被另一個窗口夾住)。

託管窗口的定義是這樣的:

<Window x:Class="GridScrollTest.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:GridScrollTest" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid x:Name="OuterGrid"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="100" /> 
      <RowDefinition Height="Auto" /> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="100" /> 
      <ColumnDefinition Width="Auto" /> 
     </Grid.ColumnDefinitions> 
     <local:SSControl x:Name="Sheet" 
         Grid.Row="1" Grid.Column="1" 
         HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Yellow" /> 
     <Canvas Grid.Row="0" Grid.Column="0" Background="LightGreen" /> 
     <Canvas Grid.Row="1" Grid.Column="0" Background="LightBlue" /> 
     <Canvas Grid.Row="0" Grid.Column="1" Background="LightCoral" /> 
    </Grid> 
</Window> 

和引用sscontrol的:

<UserControl x:Class="GridScrollTest.SSControl" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    Height="270" Width="600"> 
    <ScrollViewer 
     HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
     CanContentScroll="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"> 
     <Grid x:Name="CellGrid" ShowGridLines="False" 
       > 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="100" /> 
       <ColumnDefinition Width="100" /> 
       <ColumnDefinition Width="100" /> 
       <ColumnDefinition Width="100" /> 
       <ColumnDefinition Width="100" /> 
       <ColumnDefinition Width="100" /> 
       <ColumnDefinition Width="100" /> 
       <ColumnDefinition Width="100" /> 
       <ColumnDefinition Width="100" /> 
      </Grid.ColumnDefinitions> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="30" /> 
       <RowDefinition Height="30" /> 
       <RowDefinition Height="30" /> 
       <RowDefinition Height="30" /> 
       <RowDefinition Height="30" /> 
       <RowDefinition Height="30" /> 
       <RowDefinition Height="30" /> 
       <RowDefinition Height="30" /> 
       <RowDefinition Height="30" /> 
       <RowDefinition Height="30" /> 
       <RowDefinition Height="30" /> 
       <RowDefinition Height="30" /> 
      </Grid.RowDefinitions> 
     </Grid> 
    </ScrollViewer> 
</UserControl> 
+0

你的結果到底是什麼?你說*「內部網格不調整或適當地顯示滾動條」*,但從你的解釋,我認爲你不希望它調整大小,是嗎?我對您的描述的理解是,您希望網格以實際大小顯示,跨越窗口的邊界,並且將滾動條添加到其中,以便用戶可以滾動可查看的部分。是對的嗎?! – gehho 2010-04-27 15:33:13

回答

3

我不知道是肯定的,但試圖在Blend你的代碼後,我認爲你的問題可能是因爲您已將ColumnDefinition.WidthRowDefinition.Height設置爲Auto。嘗試將它們設置爲*,並刪除用戶控件的Height=270Width=600。這樣,外部網格填充窗口中的所有可用空間,右下角的單元格具有滾動條。

+0

將寬度和高度定義從Auto(這顯然意味着無論您需要什麼)更改爲'*'(這似乎表示剩餘空間)可以解決問題。謝謝! – 2010-04-27 17:22:05

+0

是的,這正是他們的意思。 **自動**表示單元格的內容說明需要多少空間,並且通常會獲得該空間。 **星號**表示電池應占用剩餘空間。您還可以讓一列具有'寬度=「1 *」',另一列設置爲'寬度=「2 *」'這意味着第一列將剩餘空間的三分之一,第二列另外兩列三分之二。當您需要在可調整大小的窗口中提供靈活的佈局時,此功能非常強大。 – gehho 2010-04-28 06:51:54