H
H
h1_0ne2019-06-01 01:27:32
WPF
h1_0ne, 2019-06-01 01:27:32

How to change model from view using textbox?

On the view I have a textbox. I want to make it so that when the text in it changes, the value of the line in the model changes (veiw-model, I'm not doing it strictly). The Text property naturally attached to this line. How can I get the text that I entered in the textbox?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Peter, 2019-06-01
@petermzg

<TextBox Text="{Binding UserName, UpdateSourceTrigger=PropertyChanged}" />

Binding.UpdateSourceTrigger, can take:
Source is updated immediately when the target property changes
Source is updated immediately when the target property changes and the target loses focus
The source is not updated until the BindingExpression.UpdateSource() method is called
For a TextBox, the default value is UpdateSourceTrigger = lost focus

T
tex0, 2019-06-01
@tex0

I'll add a little to Peter 's answer (what you need to know about): The binding has a Mode
property that indicates the direction of the binding. In your case, it is not necessary to write it, because. it is set to the default value (TwoWay), indicating that the binding will be bidirectional - from the model to the view (when the value changes programmatically, it is displayed on the view) and back (when in the view, for example, in the control (in your case TextBox), you change the value, it goes into the bound property of the model)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question