title: Binding the Width of columns to DependencyProperty with a IValueConverter
body: <p>I am trying to make the width of a column in a Grid element variable. For this I have a DependencyProperty "ItemWidth" and bind the Width-element from the Button to this DP. Because of the TwoWay-Binding, I need a converter that converts doubles to DataGridLength.</p>
  
  <p>My MainWindow.xaml looks like this:</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication1.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:utils="clr-namespace:WpfApplication1" Title="MainWindow" Height="350" Width="525"&gt;
  &lt;Grid&gt;
      &lt;Grid.ColumnDefinitions&gt;
          &lt;ColumnDefinition /&gt;
          &lt;ColumnDefinition /&gt;
      &lt;/Grid.ColumnDefinitions&gt;
      &lt;Grid.Resources&gt;
          &lt;utils:ColumnWidthConverter x:Key="columnWidthConverter"/&gt;
      &lt;/Grid.Resources&gt;
      &lt;Button Grid.Row="0" Grid.Column="0" Width="{Binding Path=ItemWidth, Mode=TwoWay, Converter={StaticResource columnWidthConverter}}" Click="Shorter_Click"&gt;shorter&lt;/Button&gt;
      &lt;Button Grid.Row="0" Grid.Column="1" Width="{Binding Path=ItemWidth, Mode=TwoWay, Converter={StaticResource columnWidthConverter}}" Click="Longer_Click"&gt;longer&lt;/Button&gt;
  &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>The ColumnWidthConverter.cs is as follows:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows;
  
  namespace WpfApplication1
  {
      class ColumnWidthConverter : IValueConverter
      {
          public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              if (value == null)
                  return null;
              else
              {
                  DataGridLengthConverter cv = new DataGridLengthConverter();
                  object result = cv.ConvertFrom(value);
                  return result;
              }
  
          }
  
          public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              if (value == null)
                  return null;
              else
              {
                  DataGridLengthConverter cv = new DataGridLengthConverter();
                  return cv.ConvertTo(value, typeof(double));
              }
          }
      }
  }
  </code></pre>
  
  <p>And the MainWindow.xaml.cs looks like this:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  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 WpfApplication1
  {
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
          }
  
          public double ItemWidth
          {
              get { return (double)GetValue(ItemWidthProperty); }
              set { SetValue(ItemWidthProperty, value); }
          }
  
          public static readonly DependencyProperty ItemWidthProperty =
          DependencyProperty.Register("ItemWidth", typeof(double), typeof(MainWindow), new UIPropertyMetadata(0.0));
  
          private void Shorter_Click(object sender, RoutedEventArgs e)
          {
              this.ItemWidth -= 100;
          }
  
          private void Longer_Click(object sender, RoutedEventArgs e)
          {
              this.ItemWidth += 100;
          }
      }
  }
  </code></pre>
  
  <p>So the Width of the Buttons should change when I click one of the Buttons. But this does not happen. Can you tell me why that is and some sort of solution?</p>