Channels ▼
RSS

Web Development

Silverlight 2 Data Binding


Choosing a Binding Mode

The Binding class gives you the ability to determine how data can flow between the source and the target. This flow can be controlled by setting the Mode property of a Binding instance. This property represents one of the three options available in the BindingMode enumerator. This enumerator exposes the OneTime, OneWay, and TwoWay options.

OneTime

The OneTime binding mode sets the target property to the source property when a binding is initially made. When this BindingMode is used, any changes to the data source will not be automatically sent to the target. Instead, the target will only be set when the source is initialized as shown in Figure 2.

Figure 2: A conceptual view of OneTime binding to a data source.

Figure 2 shows the simplistic binding nature of the OneTime BindingMode. As you can imagine, this BindingMode is appropriate in situations where you only care about the initial value of a property. For instance, you may want to display the "creation date" of a database record. Because this value should not change, the OneTime BindingMode is a great choice. However, for property values that will change, like the date/time that a database record was last modified, you may want to use the OneWay binding option.

OneWay

The OneWay BindingMode is the default option used when you create a Binding. This option gives you the ability to automatically receive changes from a source property. This means that whenever the binding source property changes, the target property will automatically change. However, the source property will not change if the target is altered. Either way, this change process is shown in Figure 3.

Figure 3: A conceptual view of OneWay binding to a data source.

Figure 3 shows how the OneWay BindingMode works at a high level. Alternatively, you may think of the speedometer in your car as OneWay binding from your gas pedal. Essentially, when you press or release your foot from the gas pedal, the speedometer changes. However, if you somehow changed the value of the speedometer itself, your gas pedal would not change. This inability to send a change from the target back to the source shows how OneWay binding works. For situations where you do want to send changes in the target back to the source, you can use the TwoWay option.

TwoWay

TwoWay binding enables two properties that are bound to each other, to change each other. At first glance, this may sound recursive, however it's not. Essentially a TwoWay binding changes the target when the source changes. On the other hand, if the target changes, the source will be updated. This can be seen in Figure 4.

Figure 4: A conceptual view of TwoWay binding to a data source.

Figure 4 shows a conceptual view of TwoWay binding. This binding approach is useful for web-based forms using Silverlight. The reason why is because forms generally allow users to add, as well as edit, data. This process of editing pre-existing data practically begs for TwoWay binding.

The TwoWay BindingMode is one of the options available to control the flow of your data. The other alternatives are available through the OneWay and OneTime options. Collectively, these options are an important part of setting up a binding. However, after the target and binding mode have been selected, you need to choose an appropriate source.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 
Dr. Dobb's TV