在wpf中最大化窗口时如何在画布上居中?

时间:2012-07-13 04:04:32

标签: wpf xaml

我在wpf中创建了一个网格。当窗口最大化时网格大小增加。所以当我最大化窗口时,我希望将网格居中,增加网格大小。这就是我写的代码。

<Grid x:Name="LayoutRoot">
    <Grid Margin="188,69,179,172" Background="#FF1B9B7B"/>
    <Rectangle Fill="#FF971A92" Height="70" Margin="233,139,315,0" Stroke="Black" VerticalAlignment="Top"/>
    <Rectangle Fill="#FF97311A" Height="70" Margin="0,139,214,0" Stroke="Black" VerticalAlignment="Top" HorizontalAlignment="Right" Width="84"/>
</Grid>

1 个答案:

答案 0 :(得分:2)

<Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid Margin="188,69,179,172" Background="#FF1B9B7B" Grid.Row="1" Grid.Column="1" Width="100" Height="100"/>
    <Rectangle Fill="#FF971A92" Height="70" Margin="233,139,315,0" Stroke="Black" VerticalAlignment="Top"/>
    <Rectangle Fill="#FF97311A" Height="70" Margin="0,139,214,0" Stroke="Black" VerticalAlignment="Top" HorizontalAlignment="Right" Width="84"/>
</Grid>

别忘了为内部网格指定高度和宽度。我希望这会有所帮助。