title: Dynamic Binding in WPF DataGridCell Template
body: <p>I have a question about data binding DataGrid in WPF. I am using the VS 2010 Beta 2 which has its own DataGrid, not the Toolkit one, although I think it is pretty much the same.</p>
  
  <p>I want to bind to a dataset which has 52 columns, one for every week of the year. For this reason I want to bind the data dynamically rather than specifying each field. The value for each field is true or false depending on some condition. Based on this value I want to show an image in the cell template if the condition is true and hide it if the condition is not true. </p>
  
  <p>My problem is that all the examples of using templates that I have found refer to the case of fixed, predefined fields, where you can have a binding like Text ="{Binding UserName}". This is no good to me because I don't know what the field names will be at design time.</p>
  
  <p>I have made up a simplified example which illustrates the problem. In this example a data table is generated which contains true and false values. The Image in my template is never visible. How would I make it invisible depending on the true or false value in the data?</p>
  
  <p></p>
  
  <pre><code>&lt;Window.Resources&gt;
  
      &lt;!--This is the bit that doesn't work...--&gt;
      &lt;Style TargetType="{x:Type Image}" x:Key="HideWhenFalse"&gt;
          &lt;Setter Property="Visibility" Value="Hidden" /&gt;
          &lt;Style.Triggers&gt;
              &lt;DataTrigger
          Binding="{Binding Path=???}" 
          Value="True"&gt; &lt;!--What to put for the path? --&gt;
                  &lt;Setter Property="Visibility"&gt;
                      &lt;Setter.Value&gt;
                          Visible
                      &lt;/Setter.Value&gt;
                  &lt;/Setter&gt;
              &lt;/DataTrigger&gt;
          &lt;/Style.Triggers&gt;
      &lt;/Style&gt;
      &lt;!--Up to here--&gt; 
  
      &lt;Style x:Key="{x:Type DataGridCell}" TargetType="{x:Type DataGridCell}"&gt;
          &lt;Setter Property="Template"&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate&gt;
                      &lt;StackPanel&gt;
                          &lt;Image Source="Images/tick.bmp" Style="{StaticResource HideWhenFalse}"&gt;
  
                          &lt;/Image&gt;
                      &lt;/StackPanel&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
      &lt;/Style&gt;
  &lt;/Window.Resources&gt;
  
  &lt;Grid&gt;
      &lt;DataGrid 
          x:Name="myDataGrid"
          AutoGenerateColumns="True" &gt;
  
      &lt;/DataGrid&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>Code behind:</p>
  
  <p>public partial class MainWindow : Window
  {</p>
  
  <pre><code>public MainWindow()
  {
      InitializeComponent();
  
      DataTable dtTable = new DataTable();
  
      dtTable.Columns.Add("A", typeof(Boolean));
      dtTable.Columns.Add("B", typeof(Boolean));
      dtTable.Columns.Add("C", typeof(Boolean));
      dtTable.Columns.Add("D", typeof(Boolean));
      dtTable.Columns.Add("E", typeof(Boolean));
      dtTable.Columns.Add("F", typeof(Boolean));
  
      for (int i = 0; i &lt; 5; i++)
      {
          object[] oValues = new Object[dtTable.Columns.Count];
  
          for (int j = 0; j &lt; dtTable.Columns.Count; j++)
          {
              oValues[j] = (j % 2 == 1) ? true : false;
          }
  
          dtTable.Rows.Add(oValues);
      }
  
      myDataGrid.ItemsSource = dtTable.DefaultView;
      myDataGrid.Items.Refresh();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>NB This is probably obvious and I am approaching the problem in completely the wrong way. Here is a confession: I have been trying to get my head around WPF for a couple of months now and I still seem to find myself approaching EVERY problem the wrong way. I hope the penny drops soon.</p>