Skip to content

Instantly share code, notes, and snippets.

@3rt4nm4n
Last active September 27, 2021 19:47
Show Gist options
  • Save 3rt4nm4n/ed01ee90f5aed4825733a23b385e2c64 to your computer and use it in GitHub Desktop.
Save 3rt4nm4n/ed01ee90f5aed4825733a23b385e2c64 to your computer and use it in GitHub Desktop.
An easy way to resize contents in your WPF grid proportional to newsize of grid.
/*Assume that x:Name for Grid is myGrid in XAML*/
private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
myGrid.Width = e.NewSize.Width;
myGrid.Height = e.NewSize.Height;
double xc = 1, yc = 1; //xc = changed width referring to x axis, yc = changed width referring to y axis
if (e.PreviousSize.Width != 0)
xc = (e.NewSize.Width / e.PreviousSize.Width);
if (e.PreviousSize.Height != 0)
yc = (e.NewSize.Height / e.PreviousSize.Height);
ScaleTransform scale = new ScaleTransform(myGrid.LayoutTransform.Value.M11 * xc, myGrid.LayoutTransform.Value.M22 * yc);
myGrid.LayoutTransform = scale;
myGrid.UpdateLayout();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment