title: wpf - add setter from code
body: <p>I try to add button from code and add triggers and setters to it. I wolud like to create button like this:</p>
  
  <pre><code>&lt;Button Height="25" Width="100" Name="TestColorButton" Margin="10, 5, 0, 0"&gt;
      &lt;TextBlock Text="{Binding Text, ElementName=ColorTextBox}"&gt;&lt;/TextBlock&gt;
          &lt;Button.Style&gt;
              &lt;Style TargetType="{x:Type Button}"&gt;
                  &lt;Setter Property="Background" Value="{Binding Fill, ElementName=NormalRectangle}"/&gt;
                  &lt;Setter Property="Template"&gt; &lt;!-- I need this setter --&gt;
                      &lt;Setter.Value&gt;
                          &lt;ControlTemplate TargetType="{x:Type Button}"&gt;
                              &lt;Border Background="{TemplateBinding Background}"&gt;
                                  &lt;ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/&gt;
                                                      &lt;/Border&gt;
                           &lt;/ControlTemplate&gt;
                       &lt;/Setter.Value&gt;
                   &lt;/Setter&gt;
               &lt;Style.Triggers&gt;
           &lt;Trigger Property="IsMouseOver" Value="True"&gt;
               &lt;Setter Property="Background" Value="{Binding Fill, ElementName=MouseOverRectangle}"/&gt;
          &lt;/Trigger&gt;
          &lt;Trigger Property="IsPressed" Value="True"&gt;
              &lt;Setter Property="Background" Value="{Binding Fill, ElementName=ClickRectangle}"&gt;&lt;/Setter&gt;
          &lt;/Trigger&gt;
                &lt;/Style.Triggers&gt;
             &lt;/Style&gt;
         &lt;/Button.Style&gt;
      &lt;/Button&gt;
  </code></pre>
  
  <p>I found how to do everything except one setter. I checked it by comment. I tried many times and I got only something like this:</p>
  
  <pre><code>ContentPresenter contentPresenter = new ContentPresenter
          {
              HorizontalAlignment = HorizontalAlignment.Center,
              VerticalAlignment = VerticalAlignment.Center
          };
  
          Border border = new Border();
          var binding = new Binding("Background");
          binding.RelativeSource = new RelativeSource(RelativeSourceMode.Self);
          BindingOperations.SetBinding(border, BackgroundProperty, binding);
          border.Child = contentPresenter;
  
          ControlTemplate controlTemplate = new ControlTemplate(typeof (Button));
          Setter templateSetter = new Setter(TemplateProperty, controlTemplate);
  
          style.Setters.Add(templateSetter);
  </code></pre>
  
  <p>I know it can not work, but I don't know how to do it differently.</p>