title: How can I bind visibility of a datagrid column on a custom wpf control?
body: <p>I've spent the morning looking at related posts, NONE of them I've found address the exact issue I'm experiencing, although I've learned a bit more along the way.</p>
  
  <p>(Using MVVM with user controls in WPF)</p>
  
  <p>Scenario: I need to create a re-usable control which is a datagrid that shows two or three columns depending on the form requirements. I have a custom control I've already created, as well as a dependency property for hiding / showing this third column option:</p>
  
  <p>*Note: This visibility is dependent entirely on what I set the property to, I never need it to change based off of selection in other areas.</p>
  
  <pre><code>public class MyCustomControl: Control
  {
      public static readonly DependencyProperty DisplayThirdColumnProperty = DependencyProperty.Register(
                                                                                          "DisplayThirdColumn",
                                                                                          typeof(bool),
                                                                                          typeof(MyCustomControl),
                                                                                          new FrameworkPropertyMetadata(false));
  
      static MyCustomControl()
      {
          DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomControl), new FrameworkPropertyMetadata(typeof(MyCustomControl)));
      }
  
      /// &lt;summary&gt;
      /// Gets or sets a value indicating whether the the third column should display.
      /// &lt;/summary&gt;
      public bool DisplayThirdColumn
      {
          get
          {
              return (bool)this.GetValue(DisplayThirdColumnProperty);
          }
          set
          {
              this.SetValue(DisplayThirdColumnProperty, value);
          }
      }
  }
  </code></pre>
  
  <p>Here is the xaml.Generic:</p>
  
  <pre><code>&lt;CheckBoxColumn Binding="{Binding StuffInThirdColumn}"
                  Header="ThirdColumn" 
                  Visibility="{Binding DisplayThirdColumn, 
                  Converter={StaticResource BooleanToVisibilityConverter},RelativeSource={RelativeSource TemplatedParent}}"/&gt;
  </code></pre>
  
  <p>Now when I consume the control:</p>
  
  <pre><code>&lt;MyControls:MyCustomControl DisplayThirdColumn="False"/&gt;
  </code></pre>
  
  <p>My apologies if my 'newbieness' is showing, but am I missing something obvious here? When I set the Visiblity property to collapsed explicitly on the control xaml.Generic, it correctly hides the column:</p>
  
  <pre><code>&lt;CheckBoxColumn Visibility="Collapsed"..../&gt;
  </code></pre>
  
  <p>Output window seems to indicate that it cant find the element to apply it to.</p>
  
  <p>If I can't use relative source, do you know another way I can accomplish this?</p>
  
  <blockquote>
    <p>System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=DisplayThirdColumn; DataItem=null; target element is 'CheckBoxColumn' (HashCode=19379515); target property is 'Visibility' (type 'Visibility')</p>
  </blockquote>