title: WPF and powershell, need to know how to add style to a control element
body: <p>I am having some trouble setting the style to a groupbox control element.  </p>
  
  <p>I have some XAML code that has styling defined at the top of the code.  i am looking for a way to add the styling already defined to an element that i am creating dynamically.  </p>
  
  <p>Here is my styling section:</p>
  
  <pre><code>&lt;Window.Resources&gt; 
          &lt;Style x:Key="GroupBoxStyle1" TargetType="{x:Type GroupBox}"&gt;
              &lt;Setter Property="BorderBrush" Value="Blue"/&gt;
              &lt;Setter Property="BorderThickness" Value="1"/&gt;
              &lt;Setter Property="Template"&gt;
                  &lt;Setter.Value&gt;
                      &lt;ControlTemplate TargetType="{x:Type GroupBox}"&gt;
                          &lt;Grid SnapsToDevicePixels="true"&gt;
                              &lt;Grid.ColumnDefinitions&gt;
                                  &lt;ColumnDefinition Width="6"/&gt;
                                  &lt;ColumnDefinition Width="*"/&gt;
                                  &lt;ColumnDefinition Width="6"/&gt;
                              &lt;/Grid.ColumnDefinitions&gt;
                              &lt;Grid.RowDefinitions&gt;
                                  &lt;RowDefinition Height="Auto"/&gt;
                                  &lt;RowDefinition Height="Auto"/&gt;
                                  &lt;RowDefinition Height="*"/&gt;
                                  &lt;RowDefinition Height="6"/&gt;
                              &lt;/Grid.RowDefinitions&gt;
                              &lt;Border BorderBrush="Transparent" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.ColumnSpan="3" Grid.Column="0" CornerRadius="8" Grid.Row="1" Grid.RowSpan="3"/&gt;
                              &lt;ContentPresenter Grid.Column="1" Margin="{TemplateBinding Padding}" Grid.Row="2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/&gt;
                              &lt;ContentPresenter ContentSource="Header" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                                Grid.Row="1" Grid.RowSpan="2" Grid.Column="1"
                                                HorizontalAlignment="Center" VerticalAlignment="Center"
                                                RenderTransformOrigin="0.5,0.5"&gt;
                                  &lt;ContentPresenter.RenderTransform&gt;
                                      &lt;RotateTransform Angle="45"/&gt;
                                  &lt;/ContentPresenter.RenderTransform&gt;
                              &lt;/ContentPresenter&gt;
                              &lt;Border BorderBrush="Black" BorderThickness="{TemplateBinding BorderThickness}" Grid.ColumnSpan="3" CornerRadius="4" Grid.Row="1" Grid.RowSpan="3"&gt;
                                  &lt;Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3"&gt;
                                      &lt;Border BorderBrush="Red" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="2"/&gt;
                                  &lt;/Border&gt;
                              &lt;/Border&gt;
                          &lt;/Grid&gt;
                      &lt;/ControlTemplate&gt;
                  &lt;/Setter.Value&gt;
              &lt;/Setter&gt;
          &lt;/Style&gt;
      &lt;/Window.Resources&gt;
  </code></pre>
  
  <p>This is what i have for my element creation:</p>
  
  <pre><code>   $NewBox = New-Object System.Windows.Controls.GroupBox
     $NewBox.Content = "L " + $counter 
     $NewBox.Header = "t"
     $NewBox.Margin = "0"
     $NewBox.Style = "GroupBoxStyle1"
     $NewBox.Name = "FirstBox"
     [System.Windows.Controls.Grid]::SetRow($NewBox,1)
     [System.Windows.Controls.Grid]::SetColumn($NewBox,1)
  </code></pre>
  
  <p>I used the style property since that was what i could find using the 'Get-Member' command.  i have a feeling i am not using the right syntax to call the target type.  Because while i can get the groupbox to appear, i continually get the following error:</p>
  
  <p>Exception setting 'Style':  "Cannot convert the 'GroupBoxStyle1 value of type "System.String" to type "System.Windows.Style"."</p>