WPF Binding Modes

If you're like me who was only a casual user of the binding process prior to WPF, you would bind a control to some data and call it a day. It was either bound or it wasn't. Well there is a little more to it than that and we'll see why that is a good thing. There are 4 main scenarios that where binding is of powerful use to the programmer. These four methods correspond to 4 binding modes and can be summarized as follows:


OneTime

one time binding

This binding will take the data from the source and bind it to the target once and only once. From then on, no changes to the source will update the target. This is good for situations where a control may need an initial value but nothing more.


OneWay

one way binding

This binding will take the data from the source and bind it to the target. From that point on, any changes to the source (Data) will reflect in the target (UI). This is good for read-only controls such as a label in a status bar that indicate the number of characters entered.


OneWayToSource

one way to source binding

This binding will take data from the target and bind it to the source. This is like OneWay binding reversed. An example use for this would be when there is something behind the scenes that needs the latest data from the UI and yet is never modified directly in the code.


TwoWay

two way binding

This binding will take data from the source and update the target as well as take input from the target and update the source. A common use for this would be if one of your users was modifying their existing profile.


It is also important to know that when the binding is not specified, a default binding will take place. And that default binding is different depending upon the type of control being bound. A TextBlock for example has a default binding of OneWay, whereas a TextBox has a default binding of TwoWay. To avoid confusion it is sometimes best to indicate your binding mode explicitly.


XAML

kick it on DotNetKicks.com

comments
1

http://google.com

commentPosted by tadd :: 3/23/2010 8:20:49 AM

Add a new comment



CAPTCHA image
Enter the code shown:
not case sensitive
refresh image