user2001803
user2001803

Reputation: 11

Why does the Window-margin used in Designer, get smaller in the running Program?

When using the Designer in Visual Studio 2013 on Windows 8.1, and creating a simple Window with a textbox with 10p margin to the right and the bottom, the margin changes as soon I run the program. How would i avoid this?

Example image: (I can't post images yet, so heres the bare link)

https://i.sstatic.net/LCrXL.jpg

https://i.sstatic.net/PUceC.jpg

Another image explaining it even better.

As you can see in the image above, the margin changes dramatically when running the program.

My Windows XAML Code is:

<Window x:Class="Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="400" Width="800" Focusable="False" WindowStartupLocation="CenterScreen" ResizeMode="CanMinimize" UseLayoutRounding="False">
<Grid>
    <TextBox HorizontalAlignment="Left" Height="351" Margin="10,10,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="774"/>
</Grid>

XAML Code of the other image:

<Window x:Class="Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="400" Width="800" Focusable="False" WindowStartupLocation="CenterScreen" ResizeMode="CanMinimize" UseLayoutRounding="False">
<Grid>
    <TextBox HorizontalAlignment="Left" Height="205" Margin="10,156,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="774"/>
    <Button Content="Button" HorizontalAlignment="Left" Margin="709,55,0,0" VerticalAlignment="Top" Width="75"/>
    <RadioButton Content="RadioButton" HorizontalAlignment="Left" Margin="699,101,0,0" VerticalAlignment="Top"/>
</Grid>

Upvotes: 0

Views: 423

Answers (1)

Reed Copsey
Reed Copsey

Reputation: 564861

Instead of specifying a Width and Height, just specify a margin and allow it to stretch:

<TextBox HorizontalAlignment="Stretch" Margin="10" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Stretch" />

Note that you explicitly had margins of 10,10,0,0, which mean you will only get margins on 2 sides - using a single number (10) or making all 4 numbers 10 puts an even margin all the way around.


Edit in response to your new version:

In this case, the better approach is to use the Grid layout features:

<Grid>
   <Grid.RowDefinitions>
       <RowDefinition Height="*" />
       <RowDefinition Height="*" />
       <RowDefinition Height="2*" />
   <Grid.RowDefinitions>
   <Grid.ColumnDefinitions>
       <ColumnDefinition Width="*" />
       <ColumnDefinition Width="Auto" />
   <Grid.ColumnDefinitions>

  <TextBox Grid.Row="2" Grid.ColumnSpan="2" Margin="10" TextWrapping="Wrap" Text="TextBox" />
  <Button Grid.Row="0" Grid.Column="1" Content="Button" Margin="10" Width="75"/>
  <RadioButton Grid.Row="1" Grid.Column="1" Content="RadioButton" Margin="10" />

</Grid>

Upvotes: 1

Related Questions