User Settings in WPF


It’s been a while since my last post, so I figured I’d write something I just recently had to work on: user settings.

In many client applications, you quickly run into the need to store application or user settings. There are a few options on how to do that, some of which are more complex than others, and I’ll provide links at the end on a other options I found. In this post, I’ll describe one of the simpler solutions, which takes advantage of existing functionality in Visual Studio, makes use of data-binding in Xaml, and also allows you to retrieve or set settings through code.

Visual Studio has design-time support for application and user settings, described quite well in the MSDN documentation. The VS designer creates as Settings class and automatically generates properties based on the setting names. Moreover, the settings will work in a ClickOnce application. As for the limitations mentioned in the article, I would add that design-time implies you need to know the user settings ahead of time. Not usually a big problem, but it can be for more complex scenarios. For example, if your application supports plug-ins that have custom settings, you wouldn’t necessarily know this ahead of time.

Assuming you’ve already created settings for your application, how could you go about making use of them in a WPF application? In order to access the properties in Xaml, you could add an entry in the application’s resource dictionary:

<Application x:Class=”SampleApp.App”
   xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
   xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
   xmlns:properties=”clr-namespace:SampleApp.Properties”
   Exit=”OnExit”
   StartupUri=”MainWindow.xaml”>
     <Application.Resources>
         <properties:Settings x:Key=”Settings” />
     </Application.Resources>
</Application>

If you had added a setting named Username as a string type, you could bind a TextBox in Xaml this way:

<TextBox Text=”{Binding Source={StaticResource Settings}, Path=Default.Username}” />

As you enter text in the TextBox, the two-way binding automatically updates the Username setting, and similarly at startup, the TextBox will be populated with the most recently saved Username value.

Finally, in order to ensure your settings are properly saved, you could implement the OnExit handler declared above, or alternatively you could put the settings in a dedicated window and save at the appropriate time.

private void OnExit(object sender, ExitEventArgs e)
{
    Properties.Settings.Default.Save();

}

To access the current property value through code:

Properties.Settings.Default[“Username”]

Now, for the additional links I promised. This article offers a more complex and complete solution. Although it’s relatively heavy-weight, it adds support for specific WPF controls, such as saving column positions in a ListView.

Comments (14)

  1. RedmondMax says:

    did you ever make your code above work ? I did not, Default.save() can NOT save it local config. file.

  2. Stomp says:

    Thanks very much for the info and the links!

  3. Gary Howlett says:

    How can you make it the binding conditional such as if the form had a "remember me" check box?

  4. Alex says:

    Great article, thanks. but i think something wrong with the text color… fix your css please, its next to impossible to read 🙂

  5. Amit says:

    Can you be more Specific

  6. Patrick Danino says:

    Amit, what is it you'd like to see clarified?

  7. www.animaonline.com says:

    Subscribe to the SettingsChanging event ( Properties.Settings.Default.SettingChanging += new System.Configuration.SettingChangingEventHandler(Default_SettingChanging);)

    then call .Save() on the settings, should make the life easier 🙂

  8. rohith says:

    hi, i have already started doing the wpf application, but now im facing the problem that for different resolutions my application is not visible properly and some of part of the applications r getting cropped,,,, i have used the grid control to place the buttons, but for other resolutions the buttons r getting displaced, so pls any help me out how to resolve it,,,,,,,,,,

  9. Mahesh says:

    I want to update values of user settings while setup (installation). I am using Visual Studio Installer MSI installer. I did following but value not updated.

    Properties.Settings.Default[“Setting1”] = "Modified Value";

    Properties.Settings.Default.Save();

  10. Xennan says:

    Actually you can access the current property value in code by using the strongly typed:

    Properties.Settings.Default.Username

    instead of: Properties.Settings.Default[“Username”]

    Hope it helps.

  11. RMittelman says:

    Great article.  Unfortunately, I can't get it to work for me. I'm a total WPF newbie.

    This entry generates an error stating that the uri refers to a namespace that is not included in the assembly.

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

    Of course I replace "SampleApp" in the entry above with my assembly name.  I can't figure out how to include a "Properties" namespace.

    Then of course, this entry generates an error stating that the type 'properties:Settings' was not found.

    <properties:Settings x:Key="Settings" />

    So what am I doing wrong?  Is there an easy way to add a namespace?

    Thanks…

  12. Deepthi says:

    Hi

    To all,

    i am developing a print template application. In this application i want to provide user specified template (on user selection). is there an idea to create and implement this type of application. Please give  me quick reply. Thank you.

  13. Rod says:

    As others have said here, the Settings property of the Property object, is causing problems. The issue is the Application.Properties class no longer supports the Settings property. Please update this article to reflect that change, or remove it entirely because at this point it is misleading.