0

In a WPF app using C#, I want to bind the default value of a CheckBox to the value of an Application Setting (Properties.Settings.Default.OpenAutomatically of type bool), such that when the form loads, the CheckBox is automatically set to the value read from the bound Application Setting.

Secondly, when the CheckBox's value changes (via the user clicking on it), the bool Application Setting should be kept in sync (true if checked, false if unchecked).

This seems like something .NET should give you out of the box without having to write code to keep the Setting in sync when a user manipulates the UI.

Thanks in advance for your help.

DiscDev
  • 38,652
  • 20
  • 117
  • 133
  • http://stackoverflow.com/questions/845030/bind-to-a-value-defined-in-the-settings – kenny Feb 06 '14 at 23:00
  • @kenny - the question you referenced talks about binding a ListBox to a Setting, which is not a bool. If I bind a CheckBox to bool, will the Setting automatically be updated as the value of the CheckBox changes? – DiscDev Feb 06 '14 at 23:17
  • I would think you can bind to a bool similarly, but I haven't tried it. Perhaps your issue is that you need to .Save() the settings somewhere else. – kenny Feb 06 '14 at 23:26
  • I don't think you'll get INotify events... if that's your concern. – kenny Feb 06 '14 at 23:29

1 Answers1

0

You can reference the namespace (YourNamespace.Properties.Settings) in your XAML and bind to Settings members but there will be no change notification (since the settings do not implement any type of change notification) so if you bind to it directly you will get the values stored in the Settings but you will not be able to update your settings by simply checking or unchecking the checkbox... although I'm not 100% sure that it would not be possible to achieve the updates as well using Triggers and Interactivity/Interactions to call the Settings.Default.Save() method...

I think you'll have to use a ViewModel, bind your CheckBox to a Property in that ViewModel and when the property changes (or a Save button is clicked) call Settings.Default.Save() method of your Settings from your ViewModel... that's how I do it, but again, I've never tried using Triggers in the View to call the Save() method because I usually have lots of settings and even if that was possible I wouldn't want all those Triggers and Method calls in my XAML anyway...

Dean Kuga
  • 11,878
  • 8
  • 54
  • 108