Rodniko
Rodniko

Reputation: 5124

resize a WPF grid when window maximized to same portions as the window

i have a basic grid in my WPF application:

<Grid>
    <Grid Height="260" HorizontalAlignment="Left" Margin="24,25,0,0" Name="grid1" VerticalAlignment="Top" Width="452">
        <Border BorderBrush="Red" BorderThickness="6"></Border>  
    </Grid>
</Grid>

the grid is in the middle of the window. When i maximize the window i want the grid to auto resize itself to the size of the window and stay with the same margin that i specified.

How do i do that ? Do i have to calculate and resize the whole thing in the resize event?

i get from this:

enter image description here

to This (i dont want this):

enter image description here

i want the grid to resize to the same portions as it was , but for a full screen.

Upvotes: 5

Views: 27879

Answers (2)

snurre
snurre

Reputation: 3105

Remove Width and Height.

<Grid>
    <Grid Margin="24,25">
        <Border BorderBrush="Red" BorderThickness="6"></Border>  
    </Grid>
</Grid>

e: Added a second grid to make it identical to OP

Upvotes: 13

Samuel Slade
Samuel Slade

Reputation: 8613

You could host the Grid inside of another Grid with something like:

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

    <!-- Main Grid -->
    <Grid Grid.Row="1" Grid.Column="1">

        <!-- Main Content -->
    </Grid>
</Grid>

That would allow you to put all your content inside of the Main Grid, while maintaining a constant margin of 25 around all edges.

Upvotes: 0

Related Questions