Note that there are some explanatory texts on larger screens.

plurals
  1. POOverride Standard Theme in App.xaml
    text
    copied!<p>I am using the standard WPF theme Aero.NormalColor.xaml. And it works very well. However for the <strong>whole application</strong>, I would like to override the Foreground color of textboxes to red.</p> <p>My first try is that</p> <pre><code>&lt;Application.Resources&gt; &lt;ResourceDictionary&gt; &lt;ResourceDictionary.MergedDictionaries&gt; &lt;ResourceDictionary Source="/PresentationFramework.Aero, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;component/themes/Aero.NormalColor.xaml"&gt; &lt;/ResourceDictionary&gt; &lt;/ResourceDictionary.MergedDictionaries&gt; &lt;Style TargetType="TextBox"&gt; &lt;Setter Property="Foreground" Value="Red" /&gt; &lt;/Style&gt; &lt;/ResourceDictionary&gt; &lt;/Application.Resources&gt; </code></pre> <p>Well... all foreground color of textboxes become red. However all textboxes lose the theme style. Yes, I know I should add "BasedOn". My second try is add "BasedOn" in the style tag.</p> <pre><code>&lt;Application.Resources&gt; &lt;ResourceDictionary&gt; &lt;ResourceDictionary.MergedDictionaries&gt; &lt;ResourceDictionary Source="/PresentationFramework.Aero, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;component/themes/Aero.NormalColor.xaml"&gt; &lt;/ResourceDictionary&gt; &lt;/ResourceDictionary.MergedDictionaries&gt; &lt;Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}"&gt; &lt;Setter Property="Foreground" Value="Red" /&gt; &lt;/Style&gt; &lt;/ResourceDictionary&gt; &lt;/Application.Resources&gt; </code></pre> <p>Exception is thrown. Same as this <a href="https://stackoverflow.com/questions/927251/wpf-extend-themes-style-stackoverflowexception">WPF : Extend Theme&#39;s style - StackOverflowException</a></p> <p>Eventually, I achieve my goal by this.</p> <p>In App.xaml</p> <pre><code>&lt;Application.Resources&gt; &lt;ResourceDictionary&gt; &lt;ResourceDictionary.MergedDictionaries&gt; &lt;ResourceDictionary Source="/PresentationFramework.Aero, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;component/themes/Aero.NormalColor.xaml"&gt; &lt;/ResourceDictionary&gt; &lt;/ResourceDictionary.MergedDictionaries&gt; &lt;/ResourceDictionary&gt; &lt;/Application.Resources&gt; </code></pre> <p>And in all windows and user control, I had to explicitly set</p> <pre><code>&lt;UserControl.Resources&gt; &lt;Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}"&gt; &lt;Setter Property="Foreground" Value="Red" /&gt; &lt;/Style&gt; &lt;/UserControl.Resources&gt; </code></pre> <p>The above code is copy and paste for many times and it is not easy to maintain. Does anyone know how to achieve my goal by just set foreground to red by <strong>one time</strong>?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload