Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

wpf

grid

resize

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.

like image 215
Rodniko Avatar asked Dec 22 '11 10:12

Rodniko


People also ask

How do I resize a grid in WPF?

If your Grid has more than one row, set the RowSpan attached property to the number of rows. Then set the HorizontalAlignment property to Left or Right (which alignment you set depends on which two columns you want to resize). Finally, set the VerticalAlignment property to Stretch.

How do I resize a control in WPF?

In WPF, you can create a resizable shape by using the Path control. Simply set up your shape's path data and then set the Stretch property to Fill and your shape will resize itself to fit the space available. It even takes into account the stroke thickness.

How do I center the grid in WPF?

Try setting your Grid's MaxWidth to 620 (the sum of your MaxWidths) or something similar. Your Grid will stretch to fill your max size, but still be small enough for the HorizontalAlignment =Center to be observable.

How do I move the grid in WPF?

Just put the grid panel inside a canvas rather than directly into the window - this will then give it X/Y co-ordinates.


1 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

like image 80
snurre Avatar answered Oct 05 '22 23:10

snurre