app.config values in default style (app.xaml)

Anyone familiar with C# has probably used application settings in one way or another. As I start out with WPF I envitably came across application wide styles that can be set up (e.g. in your app.xaml) to define how a control looks (and also behaves) like. Playing around with a simple button style, I was wondering whether there was a simple way of binding settings from app.config directly to a style.

<Style x:Key="MinimalButton" TargetType="Button">
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="FontFamily" Value="Verdana"/>
        <Setter Property="FontSize" Value="16"/>
        <Setter Property="Background" Value="Transparent"/>
</Style>

 This would allow me to have a central place (app.config) for handling some design aspects of the application while also allowing me to (re-)use part of that in code later without having to maintain the style settings in the xaml and potentially in app.config/the settings storage of your choice. As an example, maybe I want to also re-use the FontSize somewhere else, not directly related to this style. Would be nice if I could pull it from the app.config.

Apparently, you can do that as pointed out here on StackOverflow.

However, I could not make it work even though I did everything seemingly correct:

App.xaml (excerpt)

xmlns:p="clr-namespace:BestApplicationInHistory.Properties"

<Style x:Key="MinimalButton" TargetType="Button">
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="FontFamily" Value="Verdana"/>
        <Setter Property="FontSize" Value="{Binding Source={x:Static p:Settings.Default}, Path=MinBtn_FontSize}"/>
        <Setter Property="Background" Value="Transparent"/>
</Style>

Looking at my app.config and not seeing the value being read from it (simply returning „0“), I decided to google some more:

<configuration>
  <appSettings>
    <add key="MinBtn_FontSize" value="16"/>
  </appSettings>
</configuration>

Turns out that the approach in in the xaml works for applicationSettings but not for appSettings.

Frankly, I wasn’t even aware of the difference as most of the applications I work on are backend services that either get config from databases or centralized webservers, or simply use appSettings. I tried covering the differences here.

You may still use appSettings along with applicationSettings, but for the specific case of using it in XAML without any further code behind, you may have to go the applicationSettings route. There may be a way of using „appSettings“, but I sure couldn’t find one.

<configuration>
<!-- you may access this via the ConfigurationManager, but not directly in the XAML -->
<appSettings>
    <add key="MinBtn_FontSize"" value="16"/>
  </appSettings>
  <!-- you may access this directly in the XAML after importing the namespace-->
  <applicationSettings>
  <BestApplicationInHistory.Properties.Settings>
      <setting name="MinBtn_FontSize" serializeAs="String">
        <value>16</value>
      </setting>
    <BestApplicationInHistory.Properties.Settings>
  </applicationSettings>
</configuration>

Hope this helps,

/chris

Share: