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.

8

2 Answers

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

2

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.

5

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like