title: C# WPF Dynamic created Labels in a Grid dont show on the Top of the Grid
body: <p>Hi (sry for bad english) i have the problem that i create dynamic rows with label content in a grid but they dont begin at the top of the Grid.</p>
  
  <pre><code>private void SelectAccounts(string AccountUsername) //Findet Nutzer und Account ID und gibt alle Accounts des Nutzers aus //// neu machen jede reihe einzeln zurückgeben!!!!!!!
      {
          MId=database.GetID(this.MasterUsername,"MasterUser","MId"); //for database request 
          AccId = database.GetID(AccountUsername, "Accounts","AccId"); //for database request 
          AccountsList.Children.Clear(); //My Grid where i safe it
  
          int rows = database.GetRows("Accounts", "AccID"); //How many Rows i have in my table 
          for (int i = 1; i &lt;= rows; i++)
          {
              stringusername=database.GetUsernameById("Accounts","Username", i, MId); //getting the username back as string 
  
  
              if(username!="") // not evry username has the right condition so somtimes the function returns me only ""
              {
                  Label l = new Label();
                  RowDefinition newRow = new RowDefinition();
                  newRow.Height = new GridLength(40);
  
                  AccountsList.RowDefinitions.Add(newRow);
                  l.Content = username;
  
                  AccountsList.Children.Add(l);
                  l.SetValue(Grid.RowProperty, i);
              }
          } 
      }
  </code></pre>
  
  <p>the problem is when i now start the programm it looks like this:
  <a href="http://i.stack.imgur.com/Am6KY.png" rel="nofollow">enter image description here</a></p>
  
  <p>if i use the loop like this:</p>
  
  <pre><code>for (int i = 1; i &lt;= rows; i++)
  {
      Label l = new Label();
      RowDefinition newRow = new RowDefinition();
      newRow.Height = new GridLength(40);
      AccountsList.RowDefinitions.Add(newRow);
      string username= database.GetUsernameById("Accounts", "Username", i, MId);
  
      if(username!="")
      {
          l.Content = username;
  
          AccountsList.Children.Add(l);
          l.SetValue(Grid.RowProperty, i);
      }
  }
  </code></pre>
  
  <p>and it looks like this: <a href="http://i.stack.imgur.com/Lkemk.png" rel="nofollow">enter image description here</a></p>
  
  <p>I can normally add a label in the .xaml and it show normal on the top of the grid.</p>
  
  <p>MyUserControll where i want to show the rows:</p>
  
  <pre><code>&lt;UserControl x:Class="AccountManager.Login"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
           mc:Ignorable="d" 
           d:DesignHeight="500" d:DesignWidth="750"&gt;
  &lt;Grid Background="White"&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="100"/&gt;
          &lt;RowDefinition Height="400"/&gt;
      &lt;/Grid.RowDefinitions&gt;
      &lt;Grid.ColumnDefinitions&gt;
          &lt;ColumnDefinition Width="13*"/&gt;
          &lt;ColumnDefinition Width="37*"/&gt;
      &lt;/Grid.ColumnDefinitions&gt;
      &lt;Grid Grid.Row="0" Grid.Column="0"&gt;
          &lt;Label Content="Your Accounts" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="21.333" FontFamily="Tekton Pro" FontWeight="Bold"&gt;&lt;/Label&gt;
          &lt;Border BorderBrush="LightBlue" BorderThickness="1.5" HorizontalAlignment="Right"/&gt;
          &lt;Border BorderBrush="LightBlue" BorderThickness="1.5" VerticalAlignment="Bottom"/&gt;
          &lt;Border BorderBrush="Blue" BorderThickness="1.5" VerticalAlignment="Bottom" Opacity="0.2"/&gt;
          &lt;Border BorderBrush="Blue" BorderThickness="1.5" HorizontalAlignment="Right" Opacity="0.2"/&gt;
      &lt;/Grid&gt;
      &lt;Grid Grid.Row="1" Grid.Column="1"&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition Width="146*"/&gt;
              &lt;ColumnDefinition Width="348*"/&gt;
              &lt;ColumnDefinition Width="91*"/&gt;
          &lt;/Grid.ColumnDefinitions&gt;
          &lt;Grid Grid.Row="0" Grid.Column="0"&gt;
              &lt;Border BorderBrush="LightBlue" BorderThickness="1.5" HorizontalAlignment="Right"/&gt;
              &lt;Border BorderBrush="Blue" BorderThickness="1.5" Opacity="0.2" HorizontalAlignment="Right"/&gt;
          &lt;/Grid&gt;
          &lt;Grid Grid.Column="1"&gt;
  
              &lt;Grid.ColumnDefinitions&gt;
                  &lt;ColumnDefinition Width="110*"/&gt;
                  &lt;ColumnDefinition Width="191*"/&gt;
              &lt;/Grid.ColumnDefinitions&gt;
              &lt;Grid.RowDefinitions&gt;
                  &lt;RowDefinition&gt;&lt;/RowDefinition&gt;
                  &lt;RowDefinition&gt;&lt;/RowDefinition&gt;
                  &lt;RowDefinition&gt;&lt;/RowDefinition&gt;
                  &lt;RowDefinition&gt;&lt;/RowDefinition&gt;
                  &lt;RowDefinition&gt;&lt;/RowDefinition&gt;
              &lt;/Grid.RowDefinitions&gt;
              &lt;Label  Content="Benutzername:" VerticalAlignment="Center" HorizontalAlignment="Right" Height="26" Width="90"/&gt;
              &lt;TextBox x:Name="addBenutzername" Grid.Row="0" Grid.Column="1" Width="185" Height="30" HorizontalAlignment="Right" VerticalAlignment="Center" BorderBrush="Black" BorderThickness="1" /&gt;
              &lt;PasswordBox x:Name="addPw" Grid.Row="1" Grid.Column="1" Width="185" Height="30" HorizontalAlignment="Right" VerticalAlignment="Center" BorderBrush="Black" BorderThickness="1" &gt;&lt;/PasswordBox&gt;
              &lt;Label Grid.Row="1" Grid.Column="0" Content="Password:" VerticalAlignment="Center" HorizontalAlignment="Right" Height="26" Width="62"/&gt;
              &lt;Label x:Name="repeatPasswordLabel" Grid.Row="2" Grid.Column="0" Content="Repeat Password:" HorizontalAlignment="Right" VerticalAlignment="Center" Height="26" Width="102" /&gt;
              &lt;PasswordBox x:Name="addReapeatPw" Grid.Row="2" Grid.Column="1" Width="185" Height="30" HorizontalAlignment="Right" VerticalAlignment="Center" BorderBrush="Black" BorderThickness="1" /&gt;
              &lt;Label Content="Email:" Grid.Row="3" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right" Height="26" Width="41"/&gt;
              &lt;TextBox x:Name="addEmail" Grid.Row="3" Grid.Column="1" Width="185" Height="30" HorizontalAlignment="Right" VerticalAlignment="Center" BorderBrush="Black" BorderThickness="1"/&gt;
              &lt;Button Grid.Row=" 5" Grid.Column="1" Content="ADD" Height="36" HorizontalAlignment="Center" VerticalAlignment="Center" Width="80" Click="AddAccount"&gt;&lt;/Button&gt;
          &lt;/Grid&gt;
          &lt;Grid Grid.Column="2"&gt;
              &lt;Grid.RowDefinitions&gt;
                  &lt;RowDefinition&gt;&lt;/RowDefinition&gt;
                  &lt;RowDefinition&gt;&lt;/RowDefinition&gt;
                  &lt;RowDefinition&gt;&lt;/RowDefinition&gt;
                  &lt;RowDefinition&gt;&lt;/RowDefinition&gt;
                  &lt;RowDefinition&gt;&lt;/RowDefinition&gt;
                  &lt;RowDefinition&gt;&lt;/RowDefinition&gt;
                  &lt;RowDefinition&gt;&lt;/RowDefinition&gt;
                  &lt;RowDefinition&gt;&lt;/RowDefinition&gt;
              &lt;/Grid.RowDefinitions&gt;
              &lt;RadioButton x:Name="r1" Grid.Row="0" Width="70" Height="30"&gt;
                  &lt;Image Source="Bilder/Facebook.png"&gt;&lt;/Image&gt;
  
              &lt;/RadioButton&gt;
              &lt;RadioButton x:Name="r2" Grid.Row="1" Width="70" Height="30"&gt;
                  &lt;Image Source="Bilder/Skype.png"&gt;&lt;/Image&gt;
              &lt;/RadioButton&gt;
              &lt;RadioButton x:Name="r3" Grid.Row="2" Width="70" Height="30"&gt;
                  &lt;Image Source="Bilder/Youtube.png"&gt;&lt;/Image&gt;
              &lt;/RadioButton&gt;
              &lt;RadioButton x:Name="r4" Grid.Row="3" Width="70" Height="30"&gt;
                  &lt;Image Source="Bilder/Twitter.png"&gt;&lt;/Image&gt;
              &lt;/RadioButton&gt;
              &lt;RadioButton x:Name="r5" Grid.Row="4" Width="70" Height="30"&gt;
                  &lt;Image Source="Bilder/Microsoft.png"&gt;&lt;/Image&gt;
              &lt;/RadioButton&gt;
              &lt;RadioButton x:Name="r6" Grid.Row="5" Width="70" Height="30"&gt;
                  &lt;Image Source="Bilder/reddit.png"&gt;&lt;/Image&gt;
              &lt;/RadioButton&gt;
              &lt;RadioButton x:Name="r7" Grid.Row="6" Width="70" Height="30"&gt;
                  &lt;Image Source="Bilder/Origin.png"&gt;&lt;/Image&gt;
              &lt;/RadioButton&gt;
              &lt;RadioButton x:Name="r8" Grid.Row="8" Width="70" Height="30"&gt;
                  &lt;Image Source="Bilder/Steam.png"&gt;&lt;/Image&gt;
              &lt;/RadioButton&gt;
          &lt;/Grid&gt;
      &lt;/Grid&gt;
      &lt;Grid VerticalAlignment="Top" x:Name="AccountsList" Grid.Row="1" Grid.Column="0"&gt;
          &lt;Border BorderBrush="LightBlue" BorderThickness="1.5" HorizontalAlignment="Right"/&gt;
          &lt;Border BorderBrush="Blue" BorderThickness="1.5" HorizontalAlignment="Right"  Opacity="0.2"/&gt;
  
  
  
      &lt;/Grid&gt;
      &lt;Grid Grid.Row="0" Grid.Column="1"&gt;
          &lt;Border BorderBrush="LightBlue" BorderThickness="1.5" VerticalAlignment="Bottom"/&gt;
          &lt;Border BorderBrush="Blue" BorderThickness="1.5" VerticalAlignment="Bottom" Opacity="0.2"/&gt;
          &lt;Button x:Name="delete" Content="Delete" HorizontalAlignment="Left" Width="80" Height="36" Margin="85,19,0,19"/&gt;
          &lt;Button x:Name="change" Content="Change" HorizontalAlignment="Center" VerticalAlignment="Center" Width="80" Height="36"&gt;&lt;/Button&gt;
  
  
  
  
      &lt;/Grid&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>My Main Windows is:</p>
  
  <pre><code>&lt;Window x:Class="AccountManager.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="500" Width="750" MaxWidth="750" MinWidth="750" MinHeight="500" MaxHeight="500"&gt;
  &lt;Grid x:Name="Main"&gt;
      &lt;Grid&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition Width="371"&gt;&lt;/ColumnDefinition&gt;
              &lt;ColumnDefinition Width="371"&gt;&lt;/ColumnDefinition&gt;
          &lt;/Grid.ColumnDefinitions&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="80"&gt;&lt;/RowDefinition&gt;
              &lt;RowDefinition Height="190"&gt;&lt;/RowDefinition&gt;
  
          &lt;/Grid.RowDefinitions&gt;
          &lt;Label Grid.Row="0" Content="Accountmanager" HorizontalAlignment="Center"  VerticalAlignment="Center"  Background="White" FontSize="24" FontFamily="Snap ITC"/&gt;
          &lt;Grid Grid.Row="0" Grid.Column="1"&gt;
              &lt;Grid.ColumnDefinitions&gt;
                  &lt;ColumnDefinition Width="150"&gt;&lt;/ColumnDefinition&gt;
                  &lt;ColumnDefinition&gt;&lt;/ColumnDefinition&gt;
              &lt;/Grid.ColumnDefinitions&gt;
  
          &lt;/Grid&gt;
          &lt;Grid Grid.Row="1" x:Name="bilder"&gt;
              &lt;Grid.RowDefinitions&gt;
                  &lt;RowDefinition&gt;&lt;/RowDefinition&gt;
                  &lt;RowDefinition&gt;&lt;/RowDefinition&gt;
              &lt;/Grid.RowDefinitions&gt;
              &lt;Grid.ColumnDefinitions&gt;
                  &lt;ColumnDefinition&gt;&lt;/ColumnDefinition&gt;
                  &lt;ColumnDefinition&gt;&lt;/ColumnDefinition&gt;
                  &lt;ColumnDefinition&gt;&lt;/ColumnDefinition&gt;
                  &lt;ColumnDefinition&gt;&lt;/ColumnDefinition&gt;
              &lt;/Grid.ColumnDefinitions&gt;
  
              &lt;Image Grid.Row="0" Source="Bilder/Facebook.png" Height="70" HorizontalAlignment="Center" VerticalAlignment="Center" Width="70"/&gt;
              &lt;Image Grid.Row="0" Grid.Column="1" Source="Bilder/Skype.png" VerticalAlignment="Center" HorizontalAlignment="Center"/&gt;
              &lt;Image Grid.Row="0" Grid.Column="2" Source="Bilder/Youtube.png" VerticalAlignment="Center" HorizontalAlignment="Center"/&gt;
              &lt;Image Grid.Row="0" Grid.Column="3" Source="Bilder/Twitter.png" HorizontalAlignment="Center" VerticalAlignment="Center"/&gt;
              &lt;Image Grid.Row="1" Grid.Column="0" Source="Bilder/Microsoft.png" HorizontalAlignment="Center" VerticalAlignment="Center"/&gt;
              &lt;Image Grid.Row="1" Grid.Column="1" Source="Bilder/reddit.png" HorizontalAlignment="Center" VerticalAlignment="Center"&gt;&lt;/Image&gt;
              &lt;Image Grid.Row="1" Grid.Column="2" Source="Bilder/Origin.png" VerticalAlignment="Center" HorizontalAlignment="Center"&gt;&lt;/Image&gt;
              &lt;Image Grid.Row="1" Grid.Column="3" Source="Bilder/Steam.png" VerticalAlignment="Center" HorizontalAlignment="Center"&gt;&lt;/Image&gt;
  
          &lt;/Grid&gt;
          &lt;Grid Grid.Row="1" Grid.Column="1"&gt;
              &lt;Grid.RowDefinitions&gt;
                  &lt;RowDefinition&gt;&lt;/RowDefinition&gt;
                  &lt;RowDefinition&gt;&lt;/RowDefinition&gt;
              &lt;/Grid.RowDefinitions&gt;
              &lt;Grid.ColumnDefinitions&gt;
                  &lt;ColumnDefinition Width="150"&gt;&lt;/ColumnDefinition&gt;
                  &lt;ColumnDefinition&gt;&lt;/ColumnDefinition&gt;
              &lt;/Grid.ColumnDefinitions&gt;
              &lt;Label  Content="Benutzername:" VerticalAlignment="Top" HorizontalAlignment="Right"/&gt;
              &lt;TextBox x:Name="benutzername" Grid.Row="0" Grid.Column="1" Width="185" Height="30" HorizontalAlignment="Left" VerticalAlignment="Top" BorderBrush="Black" BorderThickness="1"&gt;&lt;/TextBox&gt;
              &lt;PasswordBox x:Name="pw" Grid.Row="0" Grid.Column="1" Width="185" Height="30" HorizontalAlignment="Left" BorderBrush="Black" BorderThickness="1" VerticalAlignment="Bottom"/&gt;
              &lt;Label  Grid.Row="0" Grid.Column="0" Content="Password:" HorizontalAlignment="Right" VerticalAlignment="Bottom"/&gt;
              &lt;Button Grid.Column="0" Grid.Row="1" Content="Add" Width="80" Height="40" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="47,0,23,0" Click="ClickAddAccount"/&gt;
              &lt;Button Grid.Column="1" Grid.Row="1" Content="Login" Width="80" Height="40" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,23,0" Click="Login" /&gt;
              &lt;Button Grid.Column="1" Grid.Row="1" Content="Forgot Password" HorizontalAlignment="Left" VerticalAlignment="Bottom" Height="40" Width="95" Click="ForgotPw" /&gt;
  
          &lt;/Grid&gt;
  
  
  
  
  
      &lt;/Grid&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>