Automatically update application settings using bindings from VS.Net Designer

It is possible to bind a property to an existing application parameter using a constructor , so I don't need to write something like

textBox.Text = Settings.Default.Name; 

upon initialization of my form.

Now it seems that the binding ends there, i.e. if I change the value of my parameter, it will not update the value in my text box and vice versa.

Is this normal or am I missing something?

+7
c #
source share
2 answers

I will explain how I do this in Windows forms with Visual Studio 2010. It probably looks similar in newer versions.

  • Click on the component to bind (text box, check box, ...)
  • Make the component properties panel visible.
  • Expand node ApplicationSettings
  • Click the ... button to the right of (PropertyBinding)

    Get application settings for component

  • Choose which setting you want to link the control to.

Below is a more detailed guide to Learning the secrets of persistent application settings

+11
source share

Well, it depends.

First, I'm not sure what you're talking about WPF or Windows Forms, so I will not do this.

Secondly, you are not โ€œtyingโ€ anything. You take the Name value and set the Text property to this value. You set the property. This is not due to magical side effects that inextricably bind the Name property to the Text property.

Thirdly, you can change the settings, but until you save them, they will not be returned to your app.config. In a Windows Forms application, you will need to do something like this:

 // event handler for the Form.Closed event. // this.FormClosed += FormClosed; void FormClosed(object sender, FormClosedEventArgs e) { Settings.Default.Name = textBox.Text; Settings.Default.Save(); } 

In WPF, you should use the standard binding semantics (this means that you avoid the hassle of setting all property values โ€‹โ€‹when closing), but you still have a trap to close the form so that you can save () the settings.

Binding:

 <TextBox xmlns:lol="clr-namespace:MyApplication.Settings" Text="{Binding Name, Source={x:Static lol:Default}}" /> 

The call to Save () is the same as in the example with Forms, but you do not need to do anything except call Save ().

+1
source share

All Articles