title: Setting styles for DataGrid Columns
body: <p>I have a DataGrid where I'm trying to set the background of each element depending on what text is being displayed in that grid cell. The grid cells are defined as follows and accessing them works correctly: </p>
  
  <pre><code>&lt;Window.Resources&gt;
      &lt;Style TargetType="{x:Type DataGridCell}"&gt;
          &lt;EventSetter Event="MouseDown" Handler="Clicked" /&gt;
          &lt;Style.Triggers&gt;
              &lt;Trigger Property="IsSelected" Value="True"&gt;
                  &lt;Setter Property="Background" Value="Transparent"/&gt;
                  &lt;Setter Property="Foreground" Value="Black"/&gt;
                  &lt;Setter Property="BorderBrush" Value="Transparent"/&gt;
                  &lt;Setter Property="BorderThickness" Value="1"/&gt;
              &lt;/Trigger&gt;
          &lt;/Style.Triggers&gt;
      &lt;/Style&gt;
  &lt;/Window.Resources&gt;
  </code></pre>
  
  <p>The part that doesn't work is that I can't set the a style for the DataGridTextColumn, none of my setters below are doing anything. The DataGrid itself is being displayed corrrectly. </p>
  
  <pre><code>        &lt;DataGrid Name="dataGrid1" AutoGenerateColumns="False" IsReadOnly="true"&gt;
              &lt;DataGridTextColumn&gt;
                  &lt;DataGridTextColumn.ElementStyle&gt;
                      &lt;Style TargetType="{x:Type TextBlock}"&gt;
                          &lt;Setter Property="HorizontalAlignment" Value="Center" /&gt;
                          &lt;Setter Property="Background" Value="Black"/&gt;
                          &lt;Style.Triggers&gt;
                              &lt;Trigger Property="Text" Value="None"&gt;
                                  &lt;Setter Property="Background" Value="Aquamarine"/&gt;
                              &lt;/Trigger&gt;
                              &lt;Trigger Property="Text" Value="First"&gt;
                                      &lt;Setter Property="Background" Value="Blue"/&gt;
                              &lt;/Trigger&gt;
                          &lt;/Style.Triggers&gt;
                      &lt;/Style&gt;
                  &lt;/DataGridTextColumn.ElementStyle&gt;
              &lt;/DataGridTextColumn&gt;
          &lt;/DataGrid&gt;
  </code></pre>
  
  <p>Thanks in advance for any help. </p>
  
  <p><strong>EDIT 1</strong> 
  Updated with Answers to NETscape's questions.
  1) My DataGrid is showing correctly, I'm even clicking on items in the grid and modifying the data behind it. However, I don't have a DataSource set in my XAML, just a simple DataContext in my view that points to the DataGrid. </p>
  
  <p>2) I don't think that the problem is with my variable names since the setter property below that doesn't have a trigger also isn't showing:</p>
  
  <pre><code> &lt;Setter Property="HorizontalAlignment" Value="Center" /&gt;
  </code></pre>
  
  <p><strong>EDIT 2</strong>
  Again in response to NETscape's questions/comments. I didn't post my code-behind originally because, since all my objects were displaying correctly, I wasn't looking for the problem there. But I think I was mistaken. The data binding for my grid and for the columns in the grid are different. The DataContext for my grid is an array of RowObjects. The datacontext for each DataGridTextColumn is one entry in my RowObject tied to a particular day of the week.  </p>
  
  <pre><code>    DataGridTextColumn cSunday = new DataGridTextColumn();
      DataGridTextColumn cMonday = new DataGridTextColumn();
      public TimeSlice[] TimeArray = new TimeSlice[48];
  
          MyWnd.dataGrid1.DataContext = TimeArray; //sets DataContext for the DataGrid
          CultureInfo cu = new CultureInfo(App.CultureID);
  
  
          //creates one column
          cSunday.Header = cu.DateTimeFormat.GetDayName(DayOfWeek.Sunday);
          cSunday.Binding = new Binding("StatusSunday");
          Wnd.dataGrid1.Columns.Add(cSunday);
  
          cMonday.Header = cu.DateTimeFormat.GetDayName(DayOfWeek.Monday);
          cMonday.Binding = new Binding("StatusMonday");
          Wnd.dataGrid1.Columns.Add(cMonday);
  </code></pre>
  
  <p><strong>EDIT 3</strong>
  For completeness sake, adding the working solution derived fron NETscape's answer. XAML:</p>
  
  <pre><code>&lt;Window
  &lt;Window.Resources&gt;
      &lt;Style TargetType="{x:Type DataGridCell}"&gt;
          &lt;EventSetter Event="MouseDown" Handler="Clicked" /&gt;
          &lt;Style.Triggers&gt;
              &lt;Trigger Property="IsSelected" Value="True"&gt;
                  &lt;Setter Property="Background" Value="Transparent"/&gt;
                  &lt;Setter Property="Foreground" Value="Black"/&gt;
                  &lt;Setter Property="BorderBrush" Value="Transparent"/&gt;
                  &lt;Setter Property="BorderThickness" Value="1"/&gt;
              &lt;/Trigger&gt;
          &lt;/Style.Triggers&gt;
      &lt;/Style&gt;
      &lt;Style x:Key="MyDataGridTextColumnElementStyle" 
         TargetType="{x:Type TextBlock}"&gt;
          &lt;Setter Property="HorizontalAlignment" Value="Center" /&gt;
          &lt;Setter Property="Background" Value="Black"/&gt;
          &lt;Style.Triggers&gt;
              &lt;Trigger Property="Text" Value="None"&gt;
                  &lt;Setter Property="Background" Value="Aquamarine"/&gt;
              &lt;/Trigger&gt;
              &lt;Trigger Property="Text" Value="First"&gt;
                  &lt;Setter Property="Background" Value="Blue"/&gt;
              &lt;/Trigger&gt;
          &lt;/Style.Triggers&gt;
      &lt;/Style&gt;
  &lt;/Window.Resources&gt;
  &lt;Grid Name="MainView"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" &gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="Auto" /&gt;
          &lt;RowDefinition Height="*" /&gt;
      &lt;/Grid.RowDefinitions&gt;
      &lt;Border Grid.Row ="0"&gt;
      &lt;/Border&gt;
      &lt;Border Grid.Row="1"&gt;
          &lt;DataGrid Name="dataGrid1" AutoGenerateColumns="False" IsReadOnly="true"/&gt;
      &lt;/Border&gt;
  &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>And the code in my view model for setting up the first 2 days/columns:</p>
  
  <pre><code>public TimeSlice[] TimeArray = new TimeSlice[48];
  public ViewModelr(WndCalendar Wnd)
      {
  
  
          MyWnd = Wnd;
          MyWnd.DataContext = this;
          MyWnd.dataGrid1.DataContext = TimeArray;
  
          cTime.Header = "";
          cTime.Binding = new Binding("TimeofDay");
          Wnd.dataGrid1.Columns.Add(cTime);
  
          CultureInfo cu = new CultureInfo(App.CultureID);
  
          cSunday.Header = cu.DateTimeFormat.GetDayName(DayOfWeek.Sunday);
          cSunday.Binding = new Binding("StatusSunday");
          Wnd.dataGrid1.Columns.Add(cSunday);
          cSunday.ElementStyle = MyWnd.FindResource("MyDataGridTextColumnElementStyle") as Style;
  
          cMonday.Header = cu.DateTimeFormat.GetDayName(DayOfWeek.Monday);
          cMonday.Binding = new Binding("StatusMonday");
          Wnd.dataGrid1.Columns.Add(cMonday);
          cMonday.ElementStyle = MyWnd.FindResource("MyDataGridTextColumnElementStyle") as Style;
  </code></pre>