0

I'm just starting with WPF (coming from Windows Forms) and I would like to bind a TextBox's text to a class's property and I would like this to be also synchronized with the application's settings (Properties.Settings.Default). Is it possible to achieve this without code-behind? Currently I have:

 <TextBox x:Name="HostnameTextBox" Text="{Binding Path=ConnectModel.Hostname, Mode=TwoWay}" />

which successfully binds to the class's property Hostname. I have stumbled upon Multibinding but I understand that is not what I want to use in this case, right? How do I make Properties.Settings.Default.Hostname property synchronized with the TextBox and the class's property, i.e. when the TextBox.Text is changed, then both the user-defined class's property and the settings property will get updated? Is it possible to do that only in the XAML file?

3
  • Wrong, you want to use a MultiBinding with a multi-value converter. However, that totally depends on what "synchronized" actually means here.
    – Clemens
    Commented Oct 13, 2020 at 13:38
  • You basically want to update Property value in Application settings also when it gets updated in TextBox ?
    – Hammas
    Commented Oct 13, 2020 at 14:17
  • @RaoHammas Yes, I want to update a property value in app settings + update a user defined class's property whenever the TextBox's value gets changed.
    – Popa611
    Commented Oct 13, 2020 at 15:48

1 Answer 1

1

Is it possible to achieve this without code-behind?

No because the Properties.Settings.Default is not represented in the Xaml in any form; and there is no such thing as chain binding especially using only one binding.


I see three ways of doing it, one wrong and two which would be right

  1. (Wrong) Create a converter which when it gets a value changes the other property.
  2. The primary class should implement INotifyPropertyChanged and there could be code which subscribes to the properties change event and updates the Settings accordingly.
  3. In the setter of the property, change the settings when it changes.

Frankly I would go with #3 because its the least work.

8
  • This is plain wrong. You can directly bind like {Binding Path=(properties:Settings.Default).MySetting}.
    – Clemens
    Commented Oct 13, 2020 at 14:25
  • @Clemens That is called an answer...post it.
    – ΩmegaMan
    Commented Oct 13, 2020 at 14:26
  • 1
    Answer to what? The question is totally unclear. And it's not about how to bind to Settings.Default, but (perhaps) how to bind to multiple properties at the same time.
    – Clemens
    Commented Oct 13, 2020 at 14:27
  • Yes, and settings properties is not on the Xaml. Hence my answer. I have provided three work arounds. If there is a way to change an alternate value in a binding; I am all ears.
    – ΩmegaMan
    Commented Oct 13, 2020 at 14:28
  • 1
    "settings properties is not on the Xaml" - what is that supposed to mean? As shown in my previous comment, you can of course bind to these properties, directly in XAML.
    – Clemens
    Commented Oct 13, 2020 at 14:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.