2016-06-10 8 views
1

我在WPF中有以下XAML代碼。這產生與列的相同大小的網格&行(如圖1)如何在XAML中改變窗口大小時使內容具有相同的空間(寬度和高度)來調整大小?

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="1*"/> 
     <RowDefinition Height="1*"/> 
     <RowDefinition Height="1*"/> 
     <RowDefinition Height="1*"/> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="1*"/> 
     <ColumnDefinition Width="1*"/> 
     <ColumnDefinition Width="1*"/> 
     <ColumnDefinition Width="1*"/> 
    </Grid.ColumnDefinitions> 

    <TextBlock Grid.Row="0" Grid.Column="0" Text="A" Background="Green"/> 
    <TextBlock Grid.Row="1" Grid.Column="1" Text="AB" Background="Red"/> 
    <TextBlock Grid.Row="2" Grid.Column="2" Text="ABC" Background="Blue"/> 
    <TextBlock Grid.Row="3" Grid.Column="3" Text="ABCD" Background="Yellow"/> 
</Grid> 

圖1.

Figure 1.

但是,當我把這個網格在一個視框(如代碼如下),行&列的大小保持不變(如圖2所示)。

<Viewbox Stretch="Uniform"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="1*"/> 
      <RowDefinition Height="1*"/> 
      <RowDefinition Height="1*"/> 
      <RowDefinition Height="1*"/> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="1*"/> 
      <ColumnDefinition Width="1*"/> 
      <ColumnDefinition Width="1*"/> 
      <ColumnDefinition Width="1*"/> 
     </Grid.ColumnDefinitions> 

     <TextBlock Grid.Row="0" Grid.Column="0" Text="A" Background="Green"/> 
     <TextBlock Grid.Row="1" Grid.Column="1" Text="AB" Background="Red"/> 
     <TextBlock Grid.Row="2" Grid.Column="2" Text="ABC" Background="Blue"/> 
     <TextBlock Grid.Row="3" Grid.Column="3" Text="ABCD" Background="Yellow"/> 

    </Grid> 
</Viewbox> 

圖2.

Figure 2.

我怎樣才能讓這個網格行的大小相等的視框裏面&列?

+0

爲什麼viewbox?仍然 – Muds

+0

,因爲我想在更改窗口大小時調整內容大小。 –

+0

它應該按照預期與*一起工作,您是否嘗試刪除viewbox,如果不是抱歉,我仍然無法理解您的requiremtn – Muds

回答

2

可以強制大小共享這樣的:

<Grid Grid.IsSharedSizeScope="True"> 
    <Grid.RowDefinitions> 
     <RowDefinition SharedSizeGroup="A"/> 
     <RowDefinition SharedSizeGroup="A"/> 
     <RowDefinition SharedSizeGroup="A"/> 
     <RowDefinition SharedSizeGroup="A"/> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition SharedSizeGroup="B"/> 
     <ColumnDefinition SharedSizeGroup="B"/> 
     <ColumnDefinition SharedSizeGroup="B"/> 
     <ColumnDefinition SharedSizeGroup="B"/> 
    </Grid.ColumnDefinitions> 
+0

這解決了我的問題,非常感謝。 –

+0

不客氣:) –

2

這是從網上

視框兩者的ViewBox

行爲是WPF中一個非常有用的控制。如果沒有什麼比 縮小到適合可用大小的內容。它不會調整 內容的大小,但會對其進行轉換。

爲什麼在不想要的時候使用viewbox,試試另一個控件。

+0

我想調整我的內容在網格內,也想保持它們在平等的空間。 –

+0

你是什麼意思?請更改您的問題以表明您想要的內容 – Muds

+0

我相信他希望文本的大小縮小以適應單元格內容。 –

1

而不是使用一個網格嘗試使用UniformGrid的:

<Viewbox Stretch="Uniform"> 
    <UniformGrid Rows="4" Columns="4"> 
     <TextBlock Grid.Row="0" Grid.Column="0" Text="A" Background="Green"/> 
     <TextBlock /> 
     <TextBlock /> 
     <TextBlock /> 

     <TextBlock /> 
     <TextBlock Grid.Row="1" Grid.Column="1" Text="AB" Background="Red"/> 
     <TextBlock /> 
     <TextBlock /> 

     <TextBlock /> 
     <TextBlock /> 
     <TextBlock Grid.Row="2" Grid.Column="2" Text="ABC" Background="Blue"/> 
     <TextBlock /> 

     <TextBlock /> 
     <TextBlock /> 
     <TextBlock /> 
     <TextBlock Grid.Row="3" Grid.Column="3" Text="ABCD" Background="Yellow"/> 
     </UniformGrid> 
</Viewbox> 

只要確保你設置的行和列,取而代之的說每個子元素所在的行/列,您只需按順序輸入它們。

+0

您的答案解決了我的問題,但我們必須定義一些額外的「TextBlock」以根據需要製作它們。非常感謝你。無論如何,@ H.B.的回答給了我完美的工作解決方案。 –

相關問題