title: How to bind the visibility of a Datagrid Column to a Checkbox in WPF
body: <p>I have several columns in a DataGrid that I would like to be visible or hidden based on CheckBox for that column.  I searched quite a bit beforehand and found a number of different answers but in each case they either didn't work or were targeting different circumstances. </p>
  
  <p>If I understand correctly ElementName won't work because the Visual Tree isn't available.  However I have tried setting the Source and RelativeSource without any luck.  In its current state it simple returns the Error:  Unresolved reference 'filterDuration'.  The error stops the program from running.</p>
  
  <p><strong>Updated with Suggestions below and more information but still unresolved</strong></p>
  
  <p>Here is the Converter:  (it never even steps into this)</p>
  
  <pre><code>[ValueConversion(typeof(bool), typeof(Visibility))]
  public class FilterVisibilityConverter : IValueConverter
  { 
      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
          bool isSelected = System.Convert.ToBoolean(value);
  
          return isSelected ? Visibility.Visible : Visibility.Hidden;
      }
  
      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
          var v = (Visibility)value; 
          return v == Visibility.Hidden ? false : true;
      }
  }
  </code></pre>
  
  <p>Here is the CheckBox:</p>
  
  <pre><code>&lt;DataTemplate x:Key="Settings"&gt;
          &lt;DockPanel&gt;                 
              &lt;GroupBox Header="Filter Columns" DockPanel.Dock="Right"&gt;
                  &lt;ag:AutoGrid Columns="150, 150, 150" RowCount="7" RowHeight="20"&gt; 
                      &lt;CheckBox Content="Id" /&gt;
                      &lt;CheckBox Content="Desc" /&gt;
                      &lt;CheckBox x:Name="filterDuration"  Content="Duration" /&gt;                          
                  &lt;/ag:AutoGrid&gt;
              &lt;/GroupBox&gt;
          &lt;/DockPanel&gt;
      &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>Here is the DataGrid:  (Extra Columns and attributes removed for brevity)</p>
  
  <pre><code>&lt;DockPanel Grid.Row="0" Grid.Column="0"&gt;
          &lt;Expander  DockPanel.Dock="Top" Header="{x:Static p:Resources.shSettings}"  ContentTemplate="{StaticResource Settings}" IsExpanded="True" /&gt;
          &lt;Border DockPanel.Dock="Top" Background="#FF595959"&gt;
              &lt;TextBlock Text="{x:Static p:Resources.shViolations}" Style="{StaticResource SectionHeaderText}" /&gt; 
          &lt;/Border&gt;
          &lt;DataGrid IsReadOnly="True" ItemsSource="{Binding items}" SelectedItem="{Binding Path=selecteditem}" AutoGenerateColumns="False"    &gt;
              &lt;DataGrid.Columns&gt;
                  &lt;DataGridTextColumn Header="Duration" Binding="{Binding Duration}" Visibility="{Binding Source={x:Reference filterDuration}, Path=IsChecked, Converter={StaticResource FilterVisibility}}" /&gt;
                  &lt;DataGridTextColumn Header="CTG Id" Binding="{Binding Id}" /&gt;
              &lt;/DataGrid.Columns&gt;
          &lt;/DataGrid&gt;
      &lt;/DockPanel&gt;
  </code></pre>