title: Binding to the width of a ColumnDefinition
body: <p>I need some kind of table for user input. So I created a grid with defined columns and rows.
  The table is not to show data, it's purpose is just to structure inputs.</p>
  
  <p>To structure all elements, like text boxes etc. I defined all columns with a width. There will be several grids aligned in a stackpanel.</p>
  
  <p>The column definitions in XAML looks like:</p>
  
  <pre><code>&lt;Grid.ColumnDefinitions&gt;
    &lt;ColumnDefinition x:Name="widthLoopID" Width="55" /&gt;
  &lt;/Grid.ColumnDefinition&gt;
  </code></pre>
  
  <p>I referenced that from other grids like:</p>
  
  <pre><code>&lt;Grid.ColumnDefinitions&gt;
    &lt;ColumnDefinition Width="{Binding ElementName=widthLoopID, Path=Width}" /&gt;
  &lt;/GridColumnDefinition&gt;
  </code></pre>
  
  <p>That works pretty well.</p>
  
  <p>Now the difficult part. How do I do this in code-behind?</p>
  
  <p>I tried the following:</p>
  
  <pre><code>// define columns
  ColumnDefinition loopIdColumn = new ColumnDefinition();
  // setup databinding for columns
  Binding loopIdBinding = new Binding();
  // set binding for width
  loopIdBinding.ElementName = "widthLoopID";
  loopIdBinding.Path = new PropertyPath("Width");
  loopIdColumn.SetBinding(Grid.WidthProperty, loopIdBinding);
  // add definition of column
  loopGrid.ColumnDefinitions.Add(loopIdColumn);
  </code></pre>
  
  <p>But when executed I get the following error</p>
  
  <pre><code>System.Windows.Data Error: 1 : Cannot create default converter to perform 'one-way' conversions between types 'System.Windows.GridLength' and 'System.Double'. Consider using Converter property of Binding. BindingExpression:Path=Width; DataItem='ColumnDefinition' (HashCode=37457626); target element is 'ColumnDefinition' (HashCode=7871588); target property is 'Width' (type 'Double')
  
  System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='55' BindingExpression:Path=Width; DataItem='ColumnDefinition' (HashCode=37457626); target element is 'ColumnDefinition' (HashCode=7871588); target property is 'Width' (type 'Double')
  </code></pre>
  
  <p>So I need to convert "Width", but how do I do that and why that can't be performed by the default converter as it does in XAML?</p>
  
  <p>I'm pretty new to WPF and may not have understood every concept, so please give me a hint.</p>
  
  <p>Thanks in advance
  Benny </p>
  
  <p>EDIT:</p>
  
  <p>I tried the hint of the first answer. It doesn't work, because GridLengthConverter isn't from type IValueConverter. </p>
  
  <p>So I wrote a DoubleToGridLengthConverter, which converts a GridLength to a Double and vice versa.</p>
  
  <p>Now the value gets converted but the result is not the same as the result that the code does in XAML. I have the feeling that it doesn't work at all from code behind. Really strange.</p>
  
  <p>Here is the code of the converter:</p>
  
  <pre><code>using System;
  using System.Windows;
  using System.Windows.Data;
  using System.Windows.Media;
  using System.Globalization;
  using System.Diagnostics;
  
  namespace editor
  {
     [ValueConversion(typeof(Double), typeof(GridLength))]
     class DoubleToGridLengthConverter : IValueConverter
     {
         public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
         {
             // check whether a value is given
             if (value != null)
             {
                Debug.WriteLine("value is: " + value + " type: " + value.GetType());
                return (Double)((GridLength)(value)).Value;
             }
             else
             {
                 throw new ValueUnavailableException();
             }
         }
  
         public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
         {
             // check whether a value is given
             if (value != null)
             {
                 return new GridLength((Double)value);
             }
             else
             {
                 throw new ValueUnavailableException();
             }
          }
      }
  }
  </code></pre>
  
  <p>Any further hints? I can imagine that someone else already have had this problem.</p>
  
  <p>Thanks again
  Benny</p>