← All lessons

Level 1 / XAML + MVVM / lesson 3 of 6

Two-way binding and derived values

Let an edit flow back into the view model, and derive a value from it.

A one-way binding pushes data at the screen. A two-way binding also pushes edits back into the view model, which is what makes text input work. A computed property then reads that value and formats it.

Understand the idea

The direction of data flow

One-way sends a view-model value to a control. Two-way also writes the user's edits back to the view model; the effective defaults depend on the control and the UI framework.

Derived values need notification

A computed Greeting reads Name every time it is accessed. In a real app, changing Name does not automatically notify a binding to re-read Greeting; also raise PropertyChanged for Greeting when Name changes.

The preview is simplified

This simulator recomputes dependent values for the preview. That convenience must not be mistaken for a complete implementation of dependent-property notifications in WPF or MAUI.

13 lines
5 lines

Preview - simulated. Not a .NET compiler.

Greeting goes here

Step 1 of 3

In progress

Add an editable field

Add a TextBox whose Text is bound to Name with Mode=TwoWay.

  • A TextBox binds Text to Name in TwoWay mode - not done yet

Coach

The checklist above is what decides whether a step is done. The coach only explains; it never grades.

The preview is a simulated XAML surface, not WPF, WinUI, or .NET MAUI. It renders StackPanel, Grid, TextBlock, Label, TextBox, Entry, and Button, and simulates INotifyPropertyChanged and ICommand. Layout attributes such as Margin and Width are accepted but not laid out. Anything else is reported as outside the lesson.

Where the simulator stops

Typing and immediate preview updates do not prove that a real TextBox uses the same update trigger. Test when edits reach the view model, and explicitly notify dependent properties.

Try it in a real .NET project

Build a WPF view with a two-way TextBox binding to Name and a TextBlock bound to Greeting. Implement INotifyPropertyChanged so changing Name also notifies Greeting; test editing, losing focus, and the updated greeting.

Check your understanding

If Name updates through a two-way binding, will a TextBlock bound to computed Greeting always update automatically?

Show the explanation

No. Greeting may compute a fresh value when read, but the binding needs a PropertyChanged notification for Greeting when Name changes so the view knows to re-read it.

Goose School teaches Microsoft's stack. Goose Solutions is not affiliated with, endorsed by, or sponsored by Microsoft.