What is the best way to bind WPF properties to ApplicationSettings in C#? Is there an automatic way like in a Windows Forms Application? Similar to this question, how (and is it possible to) do you do the same thing in WPF?
7 Answers
You can directly bind to the static object created by Visual Studio.
In your windows declaration add:
xmlns:p="clr-namespace:UserSettings.Properties"
where UserSettings
is the application namespace.
Then you can add a binding to the correct setting:
<TextBlock Height="{Binding Source={x:Static p:Settings.Default},
Path=Height, Mode=TwoWay}" ....... />
Now you can save the settings, per example when you close your application:
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
Properties.Settings.Default.Save();
base.OnClosing(e);
}

- 4,783
- 17
- 86
- 139

- 4,083
- 2
- 21
- 17
-
2I was surprised that the `TwoWay` binding actually works in both ways. Nice. +1 – Adam Badura Nov 25 '09 at 12:56
-
10Note to searchers out there, *TwoWay* is REQUIRED or the settings won't be saved. Annoying to try and figure that one out. – Oct 21 '10 at 20:28
-
4For a slightly different syntax (and a good intro), read http://blogs.msdn.com/b/patrickdanino/archive/2008/07/23/user-settings-in-wpf.aspx – Pat Jun 23 '11 at 15:39
-
4While this is a great answer to the asked question, I've found that binding is not really the right answer for the Tools/Options dialog style of editing settings; you don't want to copy the widget values to the properties until the user clicks the OK button. Using explicit code to do this in the OK click handler means the Cancel button doesn't need a handler at all provided IsCancel == true – Peter Wone Nov 09 '11 at 06:54
-
@PeterWone to persist the settings you need to use Properties.Settings.Default.Save() from what I've read, so in your scenario at Cancel action on the property editing dialog, you would use Properties.Settings.Default.Reload() [and at OK you'd call Save] – George Birbilis Dec 10 '15 at 14:26
-
@GeorgeBirbilis - Reload will reload *all* of the values. This will almost certainly produce side-effects for software that slavishly uses binding everywhere. – Peter Wone Dec 12 '15 at 03:25
-
This method only works if the settings file is marked as public access modifier. Also In order to save the value using MyApp.Properties.Settings.Default.Save(), the setting scope has to be set to User. See http://stackoverflow.com/a/845033/194717 – Tony Jan 08 '17 at 15:18
In case you are a VB.Net developer attempting this, the answer is a smidge different.
xmlns:p="clr-namespace:ThisApplication"
Notice the .Properties isn't there.
In your binding it's MySettings.Default, instead of Settings.Default - since the app.config stores it differently.
<TextBlock Height={Binding Source={x:Static p:MySettings.Default}, Path=Height, ...
After a bit of pulling out my hair, I discovered this. Hope it helps

- 91
- 1
- 1
I like the accepted answer, I ran into a special case though. I had my text box set as "read only" so that I can change the value of it only in the code. I couldn't understand why the value wasn't propagated back to the Settings although I had the Mode as "TwoWay".
Then, I found this: http://msdn.microsoft.com/en-us/library/system.windows.data.binding.updatesourcetrigger.aspx
The default is Default, which returns the default UpdateSourceTrigger value of the target dependency property. However, the default value for most dependency properties is PropertyChanged, while the Text property has a default value of LostFocus.
Thus, if you have the text box with IsReadOnly="True" property, you have to add a UpdateSourceTrigger=PropertyChanged value to the Binding statement:
<TextBox Text={Binding Source={x:Static p:Settings.Default}, Path=myTextSetting, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged} ... />

- 1,433
- 3
- 14
- 24
The easiest way would be to bind to an object that exposes your application settings as properties or to include that object as a StaticResource and bind to that.
Another direction you could take is creation your own Markup Extension so you can simply use PropertyName="{ApplicationSetting SomeSettingName}". To create a custom markup extension you need to inherit MarkupExtension and decorate the class with a MarkupExtensionReturnType attribute. John Bowen has a post on creating a custom MarkupExtension that might make the process a little clearer.

- 83,269
- 19
- 178
- 237
Kris, I'm not sure this is the best way to bind ApplicationSettings, but this is how I did it in Witty.
1) Create a dependency property for the setting that you want to bind in the window/page/usercontrol/container. This is case I have an user setting to play sounds.
public bool PlaySounds
{
get { return (bool)GetValue(PlaySoundsProperty); }
set { SetValue(PlaySoundsProperty, value); }
}
public static readonly DependencyProperty PlaySoundsProperty =
DependencyProperty.Register("PlaySounds", typeof(bool), typeof(Options),
new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnPlaySoundsChanged)));
private static void OnPlaySoundsChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
Properties.Settings.Default.PlaySounds = (bool)args.NewValue;
Properties.Settings.Default.Save();
}
2) In the constructor, initialize the property value to match the application settings
PlaySounds = Properties.Settings.Default.PlaySounds;
3) Bind the property in XAML
<CheckBox Content="Play Sounds on new Tweets" x:Name="PlaySoundsCheckBox" IsChecked="{Binding Path=PlaySounds, ElementName=Window, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
You can download the full Witty source to see it in action or browse just the code for options window.

- 8,683
- 7
- 36
- 31
I like to do it through the ViewModel and just do the binding as normal in the XAML
public Boolean Value
{
get
{
return Settings.Default.Value;
}
set
{
Settings.Default.SomeValue= value;
Settings.Default.Save();
Notify("SomeValue");
}
}

- 479
- 5
- 12
-
I think this is the best solution for most cases, because it enforces consistency. For the UI there is no difference if the value comes from the Settings or from anywhere else. – Florian Jul 21 '14 at 10:54
Also read this article on how it is done in BabySmash
You only need to back the Settings with DO (Like Alan's example) if you need the change notification! binding to the POCO Settings class will also work!

- 17,045
- 12
- 60
- 74