HOW TO:在格線之間共用調整大小屬性
本範例說明如何共用 Grid 項目之間的資料行和資料列調整大小資料,以維持調整大小的一致性。
範例
下列範例使用兩個 Grid 項目,做為父代 DockPanel 的子項目。 Grid 的 IsSharedSizeScope 附加屬性定義於父代 DockPanel。
此範例使用兩個 Button 項目以控制屬性值,每個項目代表一個布林屬性值。 當 IsSharedSizeScope 屬性值設定為 true 時,SharedSizeGroup 的每一資料行或資料列的成員,不論資料列或資料行的內容為何,都會共用調整大小資訊。
<DockPanel Name="dp1" Grid.IsSharedSizeScope="False" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10">
...
<StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
<Button Click="setTrue" Margin="0,0,10,10">Set IsSharedSizeScope="True"</Button>
<Button Click="setFalse" Margin="0,0,10,10">Set IsSharedSizeScope="False"</Button>
</StackPanel>
<StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
<Grid ShowGridLines="True" Margin="0,0,10,0">
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="FirstColumn"/>
<ColumnDefinition SharedSizeGroup="SecondColumn"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" SharedSizeGroup="FirstRow"/>
</Grid.RowDefinitions>
<Rectangle Fill="Silver" Grid.Column="0" Grid.Row="0" Width="200" Height="100"/>
<Rectangle Fill="Blue" Grid.Column="1" Grid.Row="0" Width="150" Height="100"/>
<TextBlock Grid.Column="0" Grid.Row="0" FontWeight="Bold">First Column</TextBlock>
<TextBlock Grid.Column="1" Grid.Row="0" FontWeight="Bold">Second Column</TextBlock>
</Grid>
<Grid ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="FirstColumn"/>
<ColumnDefinition SharedSizeGroup="SecondColumn"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" SharedSizeGroup="FirstRow"/>
</Grid.RowDefinitions>
<Rectangle Fill="Silver" Grid.Column="0" Grid.Row="0"/>
<Rectangle Fill="Blue" Grid.Column="1" Grid.Row="0"/>
<TextBlock Grid.Column="0" Grid.Row="0" FontWeight="Bold">First Column</TextBlock>
<TextBlock Grid.Column="1" Grid.Row="0" FontWeight="Bold">Second Column</TextBlock>
</Grid>
</StackPanel>
<TextBlock Margin="10" DockPanel.Dock="Top" Name="txt1"/>
下列程式碼後置範例處理按鈕 Click 事件所引發的方法。 此範例將這些方法呼叫的結果寫至 TextBlock 項目,此項目使用相關的 Get 方法將新的屬性值輸出為字串。
Private Sub setTrue(ByVal sender As Object, ByVal args As RoutedEventArgs)
Grid.SetIsSharedSizeScope(dp1, True)
txt1.Text = "IsSharedSizeScope Property is set to " + Grid.GetIsSharedSizeScope(dp1).ToString()
End Sub
Private Sub setFalse(ByVal sender As Object, ByVal args As RoutedEventArgs)
Grid.SetIsSharedSizeScope(dp1, False)
txt1.Text = "IsSharedSizeScope Property is set to " + Grid.GetIsSharedSizeScope(dp1).ToString()
End Sub
private void setTrue(object sender, System.Windows.RoutedEventArgs e)
{
Grid.SetIsSharedSizeScope(dp1, true);
txt1.Text = "IsSharedSizeScope Property is set to " + Grid.GetIsSharedSizeScope(dp1).ToString();
}
private void setFalse(object sender, System.Windows.RoutedEventArgs e)
{
Grid.SetIsSharedSizeScope(dp1, false);
txt1.Text = "IsSharedSizeScope Property is set to " + Grid.GetIsSharedSizeScope(dp1).ToString();
}