title: WPF CustomControl overwriteable default style
body: <p>I have a question, about implementing default styles in a custom control in wpf.
  I have writen my own IconButton control that puts two TextBlocks that are customizeable into a button control, which mostly works. The way this looks is like this:</p>
  <pre><code>&lt;Style TargetType=&quot;{x:Type MyButton}&quot; BasedOn=&quot;{StaticResource {x:Type Button}}&quot;&gt;
          &lt;Setter Property=&quot;Template&quot;&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate TargetType=&quot;{x:Type MyButton}&quot;&gt;
                      &lt;Button&gt;
                          &lt;Button.Content&gt;
                              &lt;Grid&gt;
                                  &lt;Grid.ColumnDefinitions&gt;
                                      &lt;ColumnDefinition /&gt;
                                      &lt;ColumnDefinition /&gt;
                                  &lt;/Grid.ColumnDefinitions&gt;
                                  &lt;TextBlock
                                      Grid.Column=&quot;0&quot;
                                      [...]&gt;
                                  &lt;/TextBlock&gt;
  
                                  &lt;TextBlock
                                      Grid.Column=&quot;1&quot;
                                      VerticalAlignment=&quot;Center&quot;
                                      Text=&quot;{TemplateBinding Text}&quot;
                                      [...]&gt;
                                  &lt;/TextBlock&gt;
                              &lt;/Grid&gt;
                          &lt;/Button.Content&gt;
                      &lt;/Button&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
      &lt;/Style&gt;
  </code></pre>
  <p>In the corresponding MyButton.cs I have defined all the DependencyProperties refered to in both TextBlocks like this:</p>
  <pre><code>public static readonly DependencyProperty TextProperty = DependencyProperty.Register(nameof(Text), typeof(string), typeof(IconButton), new FrameworkPropertyMetadata(string.Empty));
  </code></pre>
  <p>All of this works. I know I can have a default value in the DepedencyProperty (here string.Empty) and if I do so it is perfectly overwriteable by defining a new value when using &quot;MyButton&quot;.
  However obviously there's also regular styles for the button like Width and Height for example and for some reason I am not able to overwrite these, when I define them in my defined style. Besides that I was planning on defining the default values for my new dependency properties in the default style as well (simply for convinience), but it doesn't seem to work either.</p>
  <p>Two things I have tried:
  (1) Use a button style (obviously would only work for the DepdendencyProperties of the button not for my new ones)</p>
  <pre><code>&lt;Button.Style&gt;
     &lt;Style TargetType=&quot;{x:Type Button}&quot;&gt;
        &lt;Setter Property=&quot;MinWidth&quot; Value=&quot;300&quot;/&gt;
        &lt;Setter Property=&quot;MinHeight&quot; Value=&quot;24&quot;/&gt;
        &lt;Setter Property=&quot;FontSize&quot; Value=&quot;12&quot;/&gt;
     &lt;/Style&gt;
  &lt;/Button.Style&gt;
  </code></pre>
  <p>or (2) define the default styles on TopLevel, i.e. just like another regular setter like I am using for the template (would work for my new DependencyProperties too):</p>
  <pre><code>&lt;Style TargetType=&quot;{x:Type MyButton}&quot; BasedOn=&quot;{StaticResource {x:Type Button}}&quot;&gt;
     &lt;Setter Property=&quot;Template&quot;&gt;...&lt;/Setter&gt;
     &lt;Setter Property=&quot;MinWidth&quot;&gt;100&lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  <p>(2) seems like the better solution as in my opinion it is supposed to do everything I want, but it doesn't work at all. Neither of my style changes are applied.
  (1) Is working, but obviously only for the buttons styles. However it is also not possible to overwrite these values, i.e., when I am using:</p>
  <pre><code> &lt;local:MyButton [...] MinWidth=&quot;100&quot;/&gt;
  </code></pre>
  <p>it does not overwrite the style which is what I would expect!</p>
  <p>Please keep in mind I am a beginner and this is my very first custom control in WPF! Thanks for all advice.</p>