WPF: How to save settings using a custom class

In this article we will show you how to read and save settings to the default settings file using a custom class.

Edit the settings file

When you create a new WPF application in Visual Studio a settings file will be automatically created for you:

Settings file
Settings file

You can open and edit it using the settings designer. This allows you to create the properties you want to have in your settings, but this can quickly get out of hand if you just use simple types in a flat structure.

I find it much more useful to encapsulate all settings in my own custom and serializable class.

Build your own settings class

As an example, let us create a class to store a window’s state.

[Serializable]
public class WindowConfig
{
    public int Height { get; set; }
    public int Width { get; set; }
    public int Left { get; set; }
    public int Top { get; set; }
    public WindowState WindowState { get; set; }
    
    public WindowConfig()
    {
    }
}

Perhaps you also want to support themes in your app:

public enum Theme
{
    Light,
    Dark,
    Blue
}

Our main config class would then look like the following:

[Serializable]
public class MyConfig
{
    public WindowConfig MainWindow { get; set; }
    public Theme Theme { get; set; }
    
    public MyConfig()
    {
        MainWindow = new WindowConfig();
        MainWindow.Width = 600;
        MainWindow.Height = 400;
  
        Theme = Theme.Light;
    }
}

All classes must be serializable for this to work properly and they need an empty constructor. I also like to set the defaults in the main constructor.

Add your custom type to the settings

The next step is to create a new property in the settings of this type:

Settings - browse type
Settings - browse type

Note: the settings designer does not shown types from its own assembly. You have two options: either have your config classes in a separate assembly or manually type the full name to the type you want in the text box.

Initialize settings on startup

The first time you run the application, the settings will be null, so you have to make sure they are created:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    
    if (Settings.Default.Config == null)
    {
        Settings.Default.Config = new MyConfig();
    }
}

As you can see, you can now access your config object anywhere in your app by accessing Settings.Default.Config. You can not only read values from this object but also update them.

If you want to save the settings back you call Settings.Default.Save();.

Bind settings to your views

Now let us make use of these settings in a useful way. We will create an extension that easily allows you to bind a property from a control to a setting.

public class SettingBindingExtension : Binding
{
    public SettingBindingExtension()
    {
        Initialize();
    }
    
    public SettingBindingExtension(string path)
        : base(path)
    {
        Initialize();
    }
    
    private void Initialize()
    {
        this.Source = Settings.Default.Config;
        this.Mode = BindingMode.TwoWay;
    }
}

And in your view you just bind settings like this:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:my="clr-namespace:WpfApplication1"
    Title="MainWindow"
    Width="{my:SettingBinding MainWindow.Width}"
    Height="{my:SettingBinding MainWindow.Height}"
    Top="{my:SettingBinding MainWindow.Top}"
    Left="{my:SettingBinding MainWindow.Left}"
    WindowState="{my:SettingBinding MainWindow.WindowState}" />

Save on exit

You can make sure settings are saved when the user exits the application:

protected override void OnExit(ExitEventArgs e)
{
    base.OnExit(e);
    Settings.Default.Save();
}
Nuno Freitas
Posted by Nuno Freitas on March 24, 2014

Related articles