title: How to hide wpf datagrid columns depending on a property
body: <p>I have the following WPF sample program:</p>
  
  <p>Xaml:</p>
  
  <pre><code>&lt;Window x:Class="AncestorArie.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="MainWindow" Height="350" Width="525"&gt;
      &lt;Window.Resources&gt;
          &lt;BooleanToVisibilityConverter x:Key="BoolToVis" /&gt;
      &lt;/Window.Resources&gt;
      &lt;Grid&gt;
          &lt;DataGrid AutoGenerateColumns="False" Name="Blumen" 
                    ItemsSource="{Binding Leaves}"&gt;
              &lt;DataGrid.Columns&gt;
                  &lt;DataGridTextColumn Binding="{Binding Color}" 
                                      Header="Farbe" Width="160" /&gt;
                  &lt;DataGridTextColumn Binding="{Binding Size}" 
                                      Header="Größe" Width="60"
                                      Visibility="{Binding Path=DataContext.Flag, 
                                                  RelativeSource={RelativeSource Findancestor, 
                                                  AncestorType={x:Type Window}}, 
                                                  Converter={StaticResource BoolToVis}}" /&gt;
              &lt;/DataGrid.Columns&gt;
          &lt;/DataGrid&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Code behind:</p>
  
  <pre><code>public partial class MainWindow : Window
  {
      public MainWindow()
      {
          InitializeComponent();
          Flowers rose = new Flowers();
          rose.Leaves = new ObservableCollection&lt;Leaf&gt;();
  
          rose.Flag = false;
  
          Leaf L1 = new Leaf();
          L1.Color = "rot";
          L1.Size = 3;
          rose.Leaves.Add(L1);
  
          Leaf L2 = new Leaf();
          L2.Color = "gelb";
          L2.Size = 2;
          rose.Leaves.Add(L2);
  
          this.DataContext = rose;            
      }
  }
  </code></pre>
  
  <p>And the model classes are:</p>
  
  <pre><code>public class Leaf
  {
      public string Color { get; set; }
      public int Size { get; set; }
  }
  
  public class Flowers
  {
      public bool Flag { get; set; }
      public ObservableCollection&lt;Leaf&gt; Leaves { get; set; }
  }
  </code></pre>
  
  <p>As you can see, I want to hide the 2nd datagrid column, if the <code>Flag</code> property is set to false. But it doesn't work. I get the following binding error in the Visual Studio Output window:</p>
  
  <blockquote>
    <p>System.Windows.Data Error: 4 : Cannot find source for binding with
    reference 'RelativeSource FindAncestor,
    AncestorType='System.Windows.Window', AncestorLevel='1''.
    BindingExpression:Path=DataContext.Flag; DataItem=null; target element
    is 'DataGridTextColumn' (HashCode=44856655); target property is
    'Visibility' (type 'Visibility')</p>
  </blockquote>
  
  <p>What is wrong in my code concerning the <code>Visibility</code> attribute?</p>