What exactly is DataBinding
How do we automatically write data to the TextBlock?
©Manning Publications Co. Please post comments or corrections to the Author Online forum: http://www.manning-sandbox.com/forum.jspa?forumID=443
One way to accomplish this would be to have the code behind updating the TextBlock when the temperature changes. Ignore for the purposes of this example how the code behind would get that information!
If the application described were truly a thermostat instead of simply a thermometer, there must be a way for the user to enter text and have the value picked up by the code behind for use in setting the desired temperature point. Once again, with sufficient work in the code behind this can be accomplished by polling for a change in the value of the TextBlock.
The scenarios explained may actually be sufficient for applications where the data changes are few and infrequent. In an application where many data values need updating, for example a grid of temperatures throughout a facility. The code behind would get very cumbersome in such a situation, and would spend quite a bit of processing time just updating the user interface.
Very simply, DataBinding associates a piece of data with a control on a form. In most cases, when the value of the data changes, the control changes in some manner. If the control is a rotary dial with 6 positions, the dial will change position based on the data value. If the control is a grid of temperature values, the temperatures of all grid locations would automatically adjust themselves as the temperatures changed without any timers or polling in the code behind.
As with the different ways of updating the data in the examples explained, there are different types of DataBinding for the various requirements.
Binding only once
If a data element must be read from a database, or a device, and the value is not going to change for the duration of the application, "OneTime" binding can be used. OneTime binding would make sense for reading a user's name from a database based upon the login credentials, for example. A friendly greeting may be displayed where the user's name is the element bound. No continued use of the user's name in the application is necessary, so reading it from the database only one time is not a burden.
Binding only in one direction
If a data element may be updated in a database or device, and those updates need to be displayed in the application but the data is never written from the application to the database or device, "OneWay"
©Manning Publications Co. Please post comments or corrections to the Author Online forum:
- So can databinding make the code easier?
DataBinding resolves the 'cumbersome code' problem.
http://www.manninq-sandbox.com/forum.jspa?forumID=443
binding may be used. OneWay binding is very appropriate for temperature displays as discussed above. A simple thermometer is a perfect example of OneWay binding. With no means or reason to modify the values, the thermometer only displays the temperature value it is sensing.
Binding in two directions
If a data element is not only read and displayed on the user's interface, but also has the capability of being modified by the user and written back to the database, "TwoWay" binding must be used. TwoWay binding is a very familiar binding concept used in all "Update User Information" forms, for example. The information is initially read from the database, but also updated back to the database from the same form.
Now that there is a basic understanding of what the types of binding are, an example each of those types will be constructed in the following sections.
Post a comment