title: How can I set Style properties on a column in an XAML datagrid using Powershell?
body: <p>I am building a WPF UI using Powershell and XAML. The particular feature I am having problems with is setting the style on the columns. I am able to set the style within the XAML, but there are 9 columns (3 columns each in 3 data grids) so I feel it would be neater to set the properties in the code rather than repeat the same block 9 times.</p>
  
  <p>This is how I set the style on a single column through XAML:</p>
  
  <pre><code>&lt;DataGridTextColumn Header="Attribute" Binding="{Binding Attribute}"&gt;
      &lt;DataGridTextColumn.ElementStyle&gt;
          &lt;Style TargetType="{x:Type TextBlock}"&gt;
              &lt;Style.Triggers&gt;
                  &lt;Trigger Property="Text" Value=""&gt;
                      &lt;Setter Property="Background" Value="LightGreen"/&gt;
                  &lt;/Trigger&gt;
              &lt;/Style.Triggers&gt;
          &lt;/Style&gt;
      &lt;/DataGridTextColumn.ElementStyle&gt;
  &lt;/DataGridTextColumn&gt;
  </code></pre>
  
  <p>This sets blank cells to light green. However, my preferred method would be to set this in Powershell.</p>
  
  <p>So far I have figured out how to manually create the columns in Powershell if they are not explicitly added in XAML. I do this like so (after binding to the controls):</p>
  
  <pre><code>$arrAllDataGrids = @($dataGridUser, $dataGridHome, $dataGridWWID)
  ForEach ($objDataGrid in $arrAllDataGrids)
  {
      $objDataGrid.AutoGenerateColumns = $false
      $colAttribute = New-Object System.Windows.Controls.DataGridTextColumn -Property @{"Header" = "Attribute"; "Binding" = New-Object System.Windows.Data.Binding "Attribute"}
      $colValue = New-Object System.Windows.Controls.DataGridTextColumn -Property @{"Header" = "Value"; "Binding" = New-Object System.Windows.Data.Binding "Value"}
      $colDate = New-Object System.Windows.Controls.DataGridTextColumn -Property @{"Header" = "Date"; "Binding" = New-Object System.Windows.Data.Binding "Date"}
      $arrColumns = @($colAttribute, $colValue, $colDate)
      ForEach ($objColumn in $arrColumns) {$objDataGrid.Columns.Add($objColumn)}
  }
  </code></pre>
  
  <p>However, I if I try and add a style through Powershell I come up against Dependency Properties. I've tried to examine them by creating the style in XAML and then trying to reverse engineer it in Powershell, this is what I've worked out so far:</p>
  
  <p>$dataGridUser.Columns[0].ElementStyle is Type System.Windows.Style
  $dataGridUser.Columns[0].ElementStyle.Triggers is Type System.Windows.Trigger
  $dataGridUser.Columns[0].ElementStyle.Triggers.Property is Type Selected.System.Windows.Trigger
  $dataGridUser.Columns[0].ElementStyle.Triggers.Property.Property is Type System.Windows.DependencyProperty</p>
  
  <p>I'm unable to create an object of type System.Windows.DependencyProperty which should hold a Name property of Text. The best I have managed so far is to create one column through XAML and then extract the Dependency property object like this:</p>
  
  <pre><code>$objDepend = $($dataGridUser.Columns[0].ElementStyle.Triggers | Select Property).Property
  </code></pre>
  
  <p>Using this object, I can then create a new column object with the correct trigger property  (I appreciate this is still missing the 'Setter' property but I think the same principles will apply) like this:</p>
  
  <pre><code>$objSelect = $(New-Object System.Windows.Trigger | Select Property)
  $objSelect.Property = $objDepend
  $objTrigger = New-Object System.Windows.Trigger 
  $objTrigger.Value = ""
  $objTrigger.Property = $objSelect.Property
  $objStyle = New-Object System.Windows.Style
  $objStyle.Triggers.Add($objTrigger)
  $colAttribute = New-Object System.Windows.Controls.DataGridTextColumn -Property @{"Header" = "Attribute"; "Binding" = New-Object System.Windows.Data.Binding "Attribute"; "ElementStyle" = $objStyle}
  </code></pre>
  
  <p>This is clearly not the right way to go about this though, I am obviously missing something, presumably around Dependency properties which I suspect of being derived rather than simply set.</p>
  
  <p>I hope that all makes sense, can anyone help?</p>