title: How to bind DataGridColumn.Visibility?
body: <p>I have an issue similar to the following post:</p>
  
  <p><a href="https://stackoverflow.com/questions/983272/silverlight-datagridtextcolumn-binding-visibility">Silverlight DataGridTextColumn Binding Visibility</a></p>
  
  <p>I need to have a Column within a Silverlight DataGrid be visibile/collapsed based on a value within a ViewModel.  To accomplish this I am attempting to Bind the Visibility property to a ViewModel.  However I soon discovered that the Visibility property is not a DependencyProperty, therefore it cannot be bound.</p>
  
  <p>To solve this, I attempted to subclass my own DataGridTextColumn. With this new class, I have created a DependencyProperty, which ultimately pushes the changes to the DataGridTextColumn.Visibility property.  This works well, if I don't databind.  The moment I databind to my new property, it fails, with a AG_E_PARSER_BAD_PROPERTY_VALUE exception.</p>
  
  <pre><code>public class MyDataGridTextColumn : DataGridTextColumn
  {
      #region public Visibility MyVisibility
  
      public static readonly DependencyProperty MyVisibilityProperty =
          DependencyProperty.Register("MyVisibility", typeof(Visibility), typeof(MyDataGridTextColumn), new PropertyMetadata(Visibility.Visible, OnMyVisibilityPropertyChanged));
  
      private static void OnMyVisibilityPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
          var @this = d as MyDataGridTextColumn;
  
          if (@this != null)
          {
              @this.OnMyVisibilityChanged((Visibility)e.OldValue, (Visibility)e.NewValue);
          }
      }
  
      private void OnMyVisibilityChanged(Visibility oldValue, Visibility newValue)
      {
          Visibility = newValue;
      }
  
      public Visibility MyVisibility
      {
          get { return (Visibility)GetValue(MyVisibilityProperty); }
          set { SetValue(MyVisibilityProperty, value); }
      }
  
      #endregion public Visibility MyVisibility
  }
  </code></pre>
  
  <p>Here is a small snippet of the XAML.</p>
  
  <pre><code>&lt;DataGrid ....&gt;
      &lt;DataGrid.Columns&gt;
          &lt;MyDataGridTextColumn Header="User Name"
                                Foreground="#FFFFFFFF"
                                Binding="{Binding User.UserName}"
                                MinWidth="150"
                                CanUserSort="True"
                                CanUserResize="False"
                                CanUserReorder="True"
                                MyVisibility="{Binding Converter={StaticResource BoolToVisibilityConverter}, Path=ShouldShowUser}"/&gt;
          &lt;DataGridTextColumn .../&gt;
      &lt;/DataGrid.Columns&gt;
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p><strong>A couple important facts.</strong>  </p>
  
  <ul>
  <li>The Converter is indeed defined above in the local resources.</li>
  <li>The Converter is correct, it is used many other places in the solution.</li>
  <li>If I replace the {Binding} syntax for the MyVisibility property with "Collapsed" the Column does in fact disappear.</li>
  <li>If I create a new DependencyProperty (i.e. string Foo), and bind to it I receive the AG_E_PARSER_BAD_PROPERTY_VALUE exception too.</li>
  </ul>
  
  <p>Does anybody have any ideas as to why this isn't working?</p>