title: Acces Textblock Text inside a ButtonStyle from codebehind
body: <p>How to access the <strong>tbRegistrationBtn.text</strong> property from <em>code behind</em> from a custom made style?</p>
  
  <p>My button is being created dynamically from codebehind and gets added to the parent control (stackpanel):
  The button gets created when i press a other button on my screen.</p>
  
  <p>Codebehind:</p>
  
  <pre><code>                Button newBtn = new Button();
                  newBtn.Width = 160;
                  newBtn.Height = 46;
                  newBtn.Style = this.FindResource("ButtonStyleRegistration") as Style;
                  spHorizontal.Children.Add(newBtn);
  </code></pre>
  
  <p>Xaml:</p>
  
  <pre><code>        &lt;Style x:Key="ButtonStyleRegistration" TargetType="{x:Type Button}"&gt;
          &lt;Setter Property="Template"&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate TargetType="{x:Type Button}"&gt;
                      &lt;Grid x:Name="registrationButton"&gt;
                          &lt;Rectangle Fill="#FF89959A" Height="Auto" RadiusY="15" RadiusX="15" Stroke="White" Width="Auto"/&gt;
                          &lt;TextBlock x:Name="tbRegistrationBtn" TextWrapping="Wrap" Text="" HorizontalAlignment="Center" Margin="7.5,14.973,0,16.84" d:LayoutOverrides="Height"/&gt;
                      &lt;/Grid&gt;
                      &lt;ControlTemplate.Triggers&gt;
                          &lt;Trigger Property="IsFocused" Value="True"/&gt;
                          &lt;Trigger Property="IsDefaulted" Value="True"/&gt;
                          &lt;Trigger Property="IsMouseOver" Value="True"/&gt;
                          &lt;Trigger Property="IsPressed" Value="True"/&gt;
                          &lt;Trigger Property="IsEnabled" Value="False"/&gt;
                      &lt;/ControlTemplate.Triggers&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
          &lt;Setter Property="FontSize" Value="10.667"/&gt;
      &lt;/Style&gt; 
  </code></pre>
  
  <p>Any attempt to retrieve the textblock results in a null error.
  Attempt:</p>
  
  <pre><code>            Style style = this.FindResource("ButtonStyleRegistration") as Style;
              newBtn.Style = style;
              TextBlock tb = (TextBlock)style.Resources.FindName("tbRegistrationBtn");
              tb.Text = "test";
  </code></pre>
  
  <p>Best regards.</p>