WPF User / Application Settings

Dianne De JesusDianne De Jesus
2 min read

If your application has custom settings, whether they be for the application as a whole or a customization for users, Microsoft already has a solution for you to store them.

You can create these settings with the VS designer, just go to the properties of your namespace and then to settings. You will be presented with a link that says create or open application settings. When clicked, a tab will open with a table that allows you to write a name for the desired setting, select a type, select a scope and write a value (can be left empty).

This will create an App.config file that stores your user values, and application settings. It also adds a folder called Properties that contains a Settings.settings file that acts as an editor and reference for the settings variables.

You can choose from a large selection of types (string, int, bool, short, byte, etc.). Scope is more limited, you can choose from user or application. User settings vary from user to user but application settings remain the same for that specific application even if the user changes. For this reason they are read-only.

These settings are meant to be implemented at design time, so even though user values can be changed during runtime, you can not add a new setting.

I referenced this article: User Settings in WPF, which helped me figure out how to use this feature.

Add a reference to the Settings resource file in the App.xaml file:

<Application.Resources>
    <properties:Settings x:Key="Setting" />
</Application.Resources>

Then you can bind the control like this:

<TextBox Text="{Binding Source={StaticResource Settings}, Path=SettingsName}" />

Alternatively you can also add the reference to your XAML window:

xmlns:properties="clr-namespace:namespaceName.Properties"

and bind the control through Static:

<TextBlock Text="Binding Source={x:Static properties: Settings.Default}, Path=SettingName}" />

In code you can access the values through the Settings class (does not vary):

Settings.Default.SettingsName

If a user setting is modified, then you need to call:

Settings.Default.Save()

If you wish to save the modification.

The article also links to some helpful resources, particularly the MSDN documentation which is a quick and helpful read and User Settings Applied which details how to implement a robust settings solutions which includes using control specific settings features. It simplifies the use of these configurations and is worth a read.

0
Subscribe to my newsletter

Read articles from Dianne De Jesus directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Dianne De Jesus
Dianne De Jesus