title: How to avoid unwanted lines of empty spaces on top of user control consists of 2D Buttons and Labels C# WPF
body: <p>For the last 3 weeks, I had been struggling to display an array of control which each user control basically comprised of 17 buttons and 2 labels. The current array size of 200 is for testing purposes only. Eventually, this piece of codes will have a programmable number of the array. 200 is also a good size to demonstrate the function of the scroll viewer. It was expected to display as follows:<a href="https://i.stack.imgur.com/Fax2G.png" rel="nofollow noreferrer">Desired output</a>. The following actual output <a href="https://i.stack.imgur.com/mu7Im.png" rel="nofollow noreferrer">Unwanted Spaces on top</a>, have unwanted lines of spaces which requires me to scroll down in order to see the start of the first element of the set of buttons and labels.
  Enclosed is the Codes for your kind attention.
  I have been programming in C# on WPF for 9 months. I had been programming in VB for years, this is my first contributing in this forum after being a consumer of the forum.
  Thank You in advance for any assistance.</p>
  
  <pre><code>&lt;Window x:Class="Test_Array_Controls_2.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:local="clr-namespace:Test_Array_Controls_2"
      mc:Ignorable="d"
      Title="MainWindow" Height="450" Width="800"&gt;
  &lt;Grid&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="*"&gt;&lt;/RowDefinition&gt;
          &lt;RowDefinition Height="100"&gt;&lt;/RowDefinition&gt;
      &lt;/Grid.RowDefinitions&gt;
      &lt;ScrollViewer VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible" VerticalAlignment="Top" Height="auto" Background="AliceBlue"&gt;
          &lt;local:UControlArray Grid.Row ="0"&gt;&lt;/local:UControlArray&gt;
      &lt;/ScrollViewer&gt;
      &lt;StackPanel Orientation="Vertical" Grid.Row="1"&gt;
          &lt;Button Width="100" HorizontalAlignment="Left" VerticalAlignment="Bottom"&gt;QQ&lt;/Button&gt;
          &lt;Button Width="100" HorizontalAlignment="Left" VerticalAlignment="Bottom"&gt;AA&lt;/Button&gt;
      &lt;/StackPanel&gt;
  
  &lt;/Grid&gt;
  </code></pre>
  
  <p>`</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  namespace Test_Array_Controls_2
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
          }
      }
  }`
  
  
  &lt;UserControl x:Class="Test_Array_Controls_2.UControlArray"
           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" 
           xmlns:local="clr-namespace:Test_Array_Controls_2"
           mc:Ignorable="d" 
           d:DesignHeight="450" d:DesignWidth="800"&gt;
  &lt;Grid Margin="0,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Name="main" Width="auto" Height="auto"&gt;
  
  &lt;/Grid&gt;&lt;/UserControl&gt;`
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  namespace Test_Array_Controls_2
  {
      /// &lt;summary&gt;
      /// Interaction logic for UControlArray.xaml
      /// &lt;/summary&gt;
      public partial class UControlArray : UserControl
      {
          private Brush[] mCritBackColour = new Brush[2] { Brushes.LightGreen, Brushes.LightCyan }; //[0] left and [1] right
          public int defaultfontsize = 10;
          static int sz = 200;
          //Button[] btn = new Button[17];
          Button[,] btn = new Button[sz, 17];
          public UControlArray()
          {
              InitializeComponent();
              int x, y;
              double left = 0, top = 0;
              for (y = 0; y &lt; sz; y++)
              {
                  Label lblleft = new Label();
                  lblleft.Width = 100;
                  lblleft.Height = 32;
                  lblleft.Margin = new Thickness(left, top, 0, 0);
                  lblleft.HorizontalAlignment = HorizontalAlignment.Left;
                  lblleft.Content = "Kiri";
                  lblleft.Background = Brushes.LightGreen;
                  main.Children.Add(lblleft);
                  left += lblleft.Width;
                  for (x = 0; x &lt; 17; x++)
                  {
  
                      btn[y, x] = new Button();
                      //left += 72;
                      btn[y, x].HorizontalAlignment = HorizontalAlignment.Left;
                      btn[y, x].Margin = new Thickness(left, top, 0, 0);
                      btn[y, x].Height = 32;
                      btn[y, x].Width = 32;
                      btn[y, x].Click += new RoutedEventHandler(Btn_click);
                      btn[y, x].Content = JMVal(x);
                      btn[y, x].ToolTip = Verbal(JMVal(x));
                      btn[y, x].FontSize = 14;
                      btn[y, x].Tag = y + ";" + x; //save information of the x and y
                      main.Children.Add(btn[y, x]);
                      left += 32;
                  }
                  //
                  Label lblright = new Label();
                  lblright.Width = 100;
                  lblright.Height = 32;
                  //left += 100;
                  lblright.Margin = new Thickness(left, top, 0, 0);
                  lblright.HorizontalAlignment = HorizontalAlignment.Left;
                  lblright.Content = "Kanan";
                  lblright.Background = Brushes.LightCyan;
                  lblright.ToolTip = y;
                  lblright.Tag = y;
                  main.Children.Add(lblright);
                  //
                  left = 0;
                  top += 64;
              }
          }
          private string Verbal(int i)
          {
              //throw new NotImplementedException();
              string[] Verb = new string[10] { "", "equal", "equal to moderate", "moderate", "moderate to strong", "strong", "strong to very strong", "very strong", "very strong to extremely strong", "extremely strong" };
              return Verb[i];
          }
  
          //
  
          private int JMVal(int i) 
          {
              if (i &gt;= 0 &amp; i &lt; 9)
              {
                  return (9 - i);
              }
              else if (i &gt;= 9 &amp; i &lt; 17)
              {
                  return (i - 7);
              }
              else return 0;
          }
          private void Btn_click(object sender, RoutedEventArgs e)
          {
              Button button = sender as Button;
              string[] InternalData = button.Tag.ToString().Split(';');
  
              ClearButtons(Convert.ToInt32(InternalData[0]));
  
              int idx = Convert.ToInt32(InternalData[1]);
              button.FontSize = 22;
              button.FontWeight = FontWeights.Bold;
  
              if (idx &gt;= 0 &amp; idx &lt;= 7) { button.Background = mCritBackColour[0]; }
              else if (idx &gt;= 9 &amp; idx &lt;= 17) { button.Background = mCritBackColour[1]; }
              else if (idx == 8) { button.Background = Brushes.OrangeRed; }
  
          }
  
          private void ClearButtons(int rw)
          {
              //throw new NotImplementedException();
              int i;
              for (i = 0; i &lt; 17; i++)
              {
                  btn[rw, i].FontSize = defaultfontsize; // defaultfontsize;
                  btn[rw, i].Background = Brushes.LightGray;
                  btn[rw, i].FontWeight = FontWeights.Light;
              }
  
          }
      }
  }
  </code></pre>