Deniz Dogan Deniz Dogan - 3 months ago 13
C# Question

How to accomplish two-way data binding in WPF?

I have heard a lot about two-way bindings in WPF, but I'm not entirely clear on how to accomplish it or what it actually means.

I have a

ListView
with a bunch of items in it. When the user selects a new item, a
TextBox
in the application will change its text to display some property of the selected item.

But when the user changes the text in the text box I want the
ListView
item to be updated immediately as well. Is there any "two-way binding" magical WPF way of accomplishing this?

Answer

If you haven't you'll need to implement INotifyPropertyChanged for your class that you're binding to.

Also, when you say you want the ListBox item to be updated immediately, you mean that you want it to change as you type in the TextBox. By default the TextBox.Text property updates its source when it loses focus, but you can change this by setting the binding UpdateSourceTrigger to PropertyChanged:

{Binding Source={...}, Path=Whatever, UpdateSourceTrigger=PropertyChanged}
Comments