title: WPF Binding to Grid Column Width
body: <p>I'm attempting to bind a <code>DependancyProperty</code> in one of my usercontrols to the <code>Width</code> property of a <code>Column</code> in a <code>Grid</code>. </p>
  
  <p>I have code similar to this:</p>
  
  <pre><code>&lt;Grid x:Name="MyGridName"&gt;
      &lt;Grid.ColumnDefinitions&gt;
          &lt;ColumnDefinition x:Name="TitleSection" Width="100" /&gt;
          &lt;ColumnDefinition Width="*" /&gt;
      &lt;/Grid.ColumnDefinitions&gt;
  
      &lt;Grid.RowDefinitions&gt;...&lt;/Grid.RowDefinitions&gt;
  
      &lt;GridSplitter x:Name="MyGridSplitter" Grid.Row="0" Grid.Column="0" ... /&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>In a separate Usercontrol I have the following<code>DependancyProperty</code> defined.</p>
  
  <pre><code>public static readonly DependencyProperty TitleWidthProperty = DependencyProperty.Register("TitleWidth", typeof(int), typeof(MyUserControl));
  
  public int TitleWidth
  {
      get { return (int)base.GetValue(TitleWidthProperty); }
      set { base.SetValue(TitleWidthProperty, value); }
  }
  </code></pre>
  
  <p>I am creating instances of the Usercontrol in code, hence I have a binding statement similar to this :</p>
  
  <pre><code>MyUserControl Cntrl = new MyUserControl(/* Construction Params */);
  BindingOperations.SetBinding(Cntrl , MyUserControl.AnotherProperty, new Binding { ElementName = "objZoomSlider", Path = new PropertyPath("Value"), Mode = BindingMode.OneWay });
  BindingOperations.SetBinding(Cntrl , MyUserControl.TitleWidthProperty, new Binding { ElementName = "TitleSection", Path = new PropertyPath("ActualWidth"), Mode = BindingMode.OneWay });
  /* Other operations on Cntrl */
  </code></pre>
  
  <p>The first binding defined works fantastically, although that is binding to an actual UIElement (in this case a Slider), but the Binding to "TitleSection" (which is the ColumnDefinition defined in the Grid) fails. Putting a breakpoint in the code and doing a watch on "TitleSection" returns the expected object. </p>
  
  <p>I am beginning to suspect that a x:Name'd ColumnDefinition can't be bound to. <strong>Can anyone suggest how I might be able to bind to the changing width of the first column in my grid?</strong></p>
  
  <p><strong>EDIT #1 - To answer comments</strong></p>
  
  <p>The databinding 'fails' in the sense that with a breakpoint set on the setter for the <code>TitleWidth</code> property, and using the GridSplitter control to resize the first column, the breakpoint is never hit. Additionally, code I would expect to be fired when the DependancyProperty <code>TitleWidth</code> changes does not get executed.</p>
  
  <p>The usercontrol is being created and added to a Stackpanel within the Grid in the <code>Window_Loaded</code> function. I would expect that the Grid has been rendered by the time the Usercontrols are being constructed. Certainly the x:Name'd Element <code>TitleSection</code> is watchable and has a value of <code>100</code> when they are being constructed / before the binding is happening.</p>
  
  <p><strong>EDIT #2 - Possibly something to do with this?</strong></p>
  
  <p>I've been having a sniff round the MSDN pages for the Grid ColumnDefinition documentation and have come across <a href="http://msdn.microsoft.com/en-us/library/system.windows.gridlength.aspx" rel="nofollow noreferrer">GridLength()</a> but I can't get my head around how I can use this in a binding expression. I cannot use the associated GridLengthConverter as a converter in the binding code as it does not derive from IValueConverter. </p>
  
  <p>I am leaning towards somehow binding to the ActualWidth property of one of the cells in the Grid object. It doesn't seem as clean as binding to the column definition, but at the moment I cannot get that to work.</p>