title: How to bind to a property on a gridviewcolumn from its celltemplate
body: <p>The general problem I want to solve is to have just one datatemplate for a checkbox which I can then use for many different columns in a listview (using a gridview). In all examples I've seen a seperate template is created for each binding which seems overkill to me.</p>
  
  <p>I've been trying to do this by creating an attached property that the gridviewcolumn will set. Then I can simply have one datatemplate for a checkbox that binds to that attached property.</p>
  
  <p>The problem I'm having is actually setting the checkbox source to the gridviewcolumn.</p>
  
  <p>Here is the xaml:</p>
  
  <pre><code>&lt;DataTemplate x:Key="CheckBoxTemplate"&gt;
      &lt;CheckBox IsChecked="{Binding Path=(ap:AttachedProperties.IsChecked),
          RelativeSource={RelativeSource AncestorType={x:Type GridViewColumn}}}" /&gt;
  &lt;/DataTemplate&gt;
  
  &lt;GridView x:Key="MyGridView"&gt;
      &lt;GridViewColumn Header="CheckBox" CellTemplate="{StaticResource CheckBoxTemplate}" ap:AttachedProperties.IsChecked="{Binding Path=SomeValue}" /&gt;
  &lt;/GridView&gt;
  </code></pre>
  
  <p><em>P.S. The Attached property is working fine, I've attached it directly to the checkbox and used a relativesource self binding and gotten the values coming through, but when I try to bind it on the GridViewColumn I get no happiness, in fact I haven't been able to bind to the Header on the GridViewColumn either...</em></p>
  
  <p><em>P.P.S I have tried other binding source expressions as well (just not the right one it seems)...</em></p>
  
  <p>Edit:
  Immediately after posting this it dawned on me that the GridViewColumn exists for all rows and so that is probably why this isn't working?!</p>
  
  <p>I've slept on it now and realise that doing things in this way wont work (because the GridViewColumn can only hold the value for the first row not each row as I would need it to) - however I still would have thought it possible to bind to the GridViewColumn?</p>
  
  <p>Here is a xaml only example to show the behaviour:</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication3.MainWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         Title="MainWindow"
         Width="525"
         Height="350"&gt;
     &lt;Grid&gt;
         &lt;ListView Name="listView1"
                   Width="479"
                   Height="287"
                   Margin="12,12,0,0"
                   HorizontalAlignment="Left"
                   VerticalAlignment="Top"&gt;
             &lt;ListView.View&gt;
                 &lt;GridView&gt;
                     &lt;GridViewColumn Header="A Header"&gt;
                         &lt;GridViewColumn.CellTemplate&gt;
                             &lt;DataTemplate&gt;
                                 &lt;StackPanel&gt;
                                     &lt;TextBlock Text="{Binding Path=Header, RelativeSource={RelativeSource AncestorType={x:Type GridViewColumn}}}" /&gt;
                                     &lt;TextBlock Text="Visible row" /&gt;
                                 &lt;/StackPanel&gt;
                             &lt;/DataTemplate&gt;
                         &lt;/GridViewColumn.CellTemplate&gt;
                     &lt;/GridViewColumn&gt;
                 &lt;/GridView&gt;
             &lt;/ListView.View&gt;
             &lt;ListViewItem /&gt;
             &lt;ListViewItem /&gt;
         &lt;/ListView&gt;
     &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Under such a scenerio I would expect to see "A Header" just above each "Visible row" text</p>