title: Binding column width programmatically in UWP?
body: <p>I am trying to create columns that can be resized by the user during runtime just like in excel. However, I am creating fields dynamically and don't know how to bind the programmatically created column's width to the width of my column created in xaml.</p>
  <p>Question: How can I bind column width programmatically rather than in the XAML?</p>
  <p>cs:</p>
  <pre><code>public sealed partial class WepPage : Page
      {
          static int i = 0;
          Grid grid;
         
          
      public WepPage()
      {
          this.InitializeComponent();
     
      }
      private void AddWepButton_Click(object sender, RoutedEventArgs e)
      {
          addWepStack();    
      }
      private void addWepStack()
      {
         
          grid = new Grid();
          grid.Name = (&quot;ItemGrid&quot; + i.ToString()); 
          WepStackPanel.Children.Add(grid);
          //create columns
          ColumnDefinition col0 = new ColumnDefinition();
          ColumnDefinition col1 = new ColumnDefinition();
          ColumnDefinition col2 = new ColumnDefinition();
          ColumnDefinition col3 = new ColumnDefinition();
          ColumnDefinition col4 = new ColumnDefinition();
          ColumnDefinition col5 = new ColumnDefinition();
          // Set the width of each column
         
          //HERE IS WHERE I WANT TO BIND THE COLUMN WIDTH TO THE WIDTH OF THE HEADERS 
          //SO THE USERS CAN CHANGE THE COLUMN WIDTH WHILE THE APPLICATION IS RUNNING.
  
          // Add columns to grid
          grid.ColumnDefinitions.Add(col0);
          grid.ColumnDefinitions.Add(col1);
          grid.ColumnDefinitions.Add(col2);
          grid.ColumnDefinitions.Add(col3);
          grid.ColumnDefinitions.Add(col4);
          grid.ColumnDefinitions.Add(col5);
  
          i++;
      }
  
  }
  </code></pre>
  <p>XAML:</p>
  <pre><code>&lt;Page NavigationCacheMode=&quot;Required&quot;
      x:Class=&quot;WASP.WepPage&quot;
      xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
      xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
      xmlns:local=&quot;using:WASP&quot;
      xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
      xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
      mc:Ignorable=&quot;d&quot;
      xmlns:controls=&quot;using:Microsoft.Toolkit.Uwp.UI.Controls&quot;
      Background=&quot;{ThemeResource ApplicationPageBackgroundThemeBrush}&quot;&gt;
  
  
      &lt;Page.Resources&gt;
          &lt;ResourceDictionary&gt;
              &lt;ResourceDictionary.MergedDictionaries&gt;
                  &lt;ResourceDictionary Source=&quot;PoleDictionary.xaml&quot; /&gt;
              &lt;/ResourceDictionary.MergedDictionaries&gt;
          &lt;/ResourceDictionary&gt;
      &lt;/Page.Resources&gt;
      &lt;Grid&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height=&quot;Auto&quot;/&gt;
              &lt;RowDefinition Height=&quot;*&quot;/&gt;
          &lt;/Grid.RowDefinitions&gt;
           &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition Width=&quot;200&quot; x:Name=&quot;WepNumColumn&quot; x:DefaultBindMode=&quot;OneWay&quot;/&gt;
              &lt;ColumnDefinition Width=&quot;Auto&quot; /&gt;
              &lt;ColumnDefinition Width=&quot;Auto&quot; /&gt;
              &lt;ColumnDefinition Width=&quot;Auto&quot; /&gt;
              &lt;ColumnDefinition Width=&quot;Auto&quot; /&gt;
              &lt;ColumnDefinition Width=&quot;Auto&quot; /&gt;
              &lt;ColumnDefinition Width=&quot;Auto&quot; /&gt;
              &lt;ColumnDefinition Width=&quot;Auto&quot; /&gt;
              &lt;ColumnDefinition Width=&quot;Auto&quot; /&gt;
              &lt;ColumnDefinition Width=&quot;Auto&quot; /&gt;
                  &lt;ColumnDefinition Width=&quot;Auto&quot; /&gt;
              &lt;/Grid.ColumnDefinitions&gt;
      
      &lt;!-- HERE IS WHERE I ADDED GRID SPLITTERS SO THE USERS CAN EDIT COLUMN WIDTH
      THIS IS WHAT I WANT THE WIDTH OF THE OTHER COLUMNS BOUND TO--&gt;
      
          &lt;controls:GridSplitter Grid.Column=&quot;1&quot;&gt;
          &lt;controls:GridSplitter.RenderTransform&gt;
              &lt;TranslateTransform X=&quot;-8&quot; /&gt;
          &lt;/controls:GridSplitter.RenderTransform&gt;
          &lt;/controls:GridSplitter&gt;
          &lt;controls:GridSplitter Grid.Column=&quot;3&quot;&gt;
              &lt;controls:GridSplitter.RenderTransform&gt;
                  &lt;TranslateTransform X=&quot;-8&quot; /&gt;
              &lt;/controls:GridSplitter.RenderTransform&gt;
          &lt;/controls:GridSplitter&gt;
          &lt;controls:GridSplitter Grid.Column=&quot;5&quot;&gt;
              &lt;controls:GridSplitter.RenderTransform&gt;
                  &lt;TranslateTransform X=&quot;-8&quot; /&gt;
              &lt;/controls:GridSplitter.RenderTransform&gt;
          &lt;/controls:GridSplitter&gt;
          &lt;controls:GridSplitter Grid.Column=&quot;7&quot;&gt;
              &lt;controls:GridSplitter.RenderTransform&gt;
                  &lt;TranslateTransform X=&quot;-8&quot; /&gt;
              &lt;/controls:GridSplitter.RenderTransform&gt;
          &lt;/controls:GridSplitter&gt;
  
          &lt;Button x:Name=&quot;addWepButton&quot; 
                  Content=&quot;+&quot; 
                  FontSize=&quot;30&quot;  
                  Grid.Column=&quot;10&quot;
                  Grid.Row=&quot;0&quot;
                  Click=&quot;AddWepButton_Click&quot; /&gt;
          &lt;TextBlock Text=&quot;WEP #&quot;
                     Grid.Column=&quot;0&quot;
                     Grid.Row=&quot;0&quot; 
                     Style=&quot;{StaticResource WepTextBlock}&quot;/&gt;
          &lt;TextBlock Text=&quot;Type&quot;
                     Grid.Column=&quot;2&quot;
                     Grid.Row=&quot;0&quot; 
                     Style=&quot;{StaticResource WepTextBlock}&quot;/&gt;
          &lt;TextBlock Text=&quot;Distance&quot;
                     Grid.Column=&quot;4&quot;
                     Grid.Row=&quot;0&quot;  
                     Style=&quot;{StaticResource WepTextBlock}&quot;/&gt;
          &lt;TextBlock Text=&quot;Direction&quot;
                     Grid.Column=&quot;6&quot;
                     Grid.Row=&quot;0&quot;  
                     Style=&quot;{StaticResource WepTextBlock}&quot;/&gt;
          &lt;TextBlock Text=&quot;Flip&quot;
                     Grid.Column=&quot;8&quot;
                     Grid.Row=&quot;0&quot;  
                     Style=&quot;{StaticResource WepTextBlock}&quot;/&gt;
  
          &lt;ScrollViewer Grid.Row=&quot;1&quot; Grid.ColumnSpan=&quot;20&quot;&gt;
              &lt;StackPanel x:Name=&quot;WepStackPanel&quot;&gt;
                  
              &lt;/StackPanel&gt;
          &lt;/ScrollViewer&gt;
      &lt;/Grid&gt;
  &lt;/Page&gt;
  </code></pre>