Philipp Lupulescu
Philipp Lupulescu

Reputation: 5

WPF - Change the Button Size and Position at Runtime - Adjust to the Window

My issue is, that I want to adjust the button size and also the position to the size of my window in WPF. I got the event:

private void Window_SizeChanged_1(object sender, SizeChangedEventArgs e)
{
   if (e.PreviousSize.Height > e.NewSize.Height )
   {
      newGameButton.Height--;

   }
   else if (e.PreviousSize.Height < e.NewSize.Height )
  {
      newGameButton.Height++;
  }

  if (e.PreviousSize.Width > e.NewSize.Width)
  {
      newGameButton.Width--;
  }
  else if (e.PreviousSize.Width < e.NewSize.Width)
  {
      newGameButton.Width++;
  }

}

Is there a posibility to set some points where the button is fixed at and grows and shrinks, depending on the windowsize?

Upvotes: 0

Views: 1300

Answers (1)

Steve
Steve

Reputation: 6444

Here is one way to accomplish it. The button, by default, has HorizontalContentAlignment="Stretch" and VerticalContentAlignment="Stretch". The grid's rows and columns re-size with the window, so the button re-sizes with the grid.

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Button Grid.Row="0" Grid.Column="0" />
</Grid>

Upvotes: 1

Related Questions