title: WPF DataGrid: Binding DataGridColumn visibility to ContextMenu MenuItems IsChecked (MVVM)
body: <p>I want to control <code>DataGrid</code> column visibility through a <code>ContextMenu</code> available to the user by right-clicking the column header. The <code>ContextMenu</code> displays the names of all available columns. I am using MVVM design pattern.</p>
  
  <p>My question is: How do I bind the <code>DataGridColumn</code>'s <code>Visibility</code> property to the <code>IsChecked</code> property of a <code>MenuItem</code> located in the <code>ContextMenu</code>.</p>
  
  <p>Some mockup code:</p>
  
  <pre><code>&lt;UserControl.Resources&gt;         
      &lt;ContextMenu x:Key="ColumnHeaderContextMenu"&gt;  
          &lt;MenuItem Header="Menu Item..1" IsCheckable="True" /&gt;  
      &lt;/ContextMenu&gt;  
      &lt;Style x:Key="ColumnHeaderStyle" 
             TargetType="{x:Type toolkit:DataGridColumnHeader}"&gt;  
          &lt;Setter Property="ContextMenu" 
                  Value="{StaticResource ColumnHeaderContextMenu}" /&gt;  
      &lt;/Style&gt;  
      &lt;BooleanToVisibilityConverter x:Key="booleanToVisibilityConverter" /&gt;  
  &lt;/UserControl.Resources&gt;  
  </code></pre>
  
  <p>...flaf flaf flaf</p>
  
  <pre><code>&lt;toolkit:DataGrid x:Name="MyGrid" AutoGenerateColumns="False" 
      ItemsSource="{Binding MyCollection, Mode=Default}" 
      EnableColumnVirtualization="True" IsReadOnly="True" 
      ColumnHeaderStyle="{StaticResource ColumnHeaderStyle}"&gt;  
      &lt;toolkit:DataGrid.Columns&gt;  
          &lt;toolkit:DataGridTextColumn Binding="{Binding Path=MyEntry}" 
              Header="MyEntry" Visibility="{Binding IsChecked, Converter=
                  {StaticResource booleanToVisibilityConverter}.... /&gt;
      &lt;/toolkit:DataGrid.Columns&gt;     
  &lt;/toolkit:DataGrid&gt;  
  </code></pre>
  
  <p>If I am being unclear please let me know and I will attempt to elaborate.</p>
  
  <p>Cheers,</p>