MSDN Magazine - December 2007 - (Page 30) attribute to OneWay. The Mode attribute deten employ OneWay when I want to display fines the binding mode that will determine read-only data to a user. I use TwoWay bindhow data flows between the source and the ing when I want the user to be able to change target. In addition to OneWay, there are three the data in the control and have that change other binding modes available: OneTime, Onereflected in the data source (a DataSet, obWayToSource, and TwoWay, ject, XML, or another bound control). I find When using OneWay binding, as shown OneWayToSource to be a good choice when I in the preceding code snippet, the data flows want to allow a user to change the data source from the source to the target each time a change without having the data source bind its data Figure 3 TwoWay Binding in Action is made on the source. And while I explicitly back to the target. I have used OneTime bindspecified this binding mode in my example, OneWay binding is the ing when I was tasked with showing, in a read-only control, the default binding mode for the TextBlock’s Text property and does state of the data as it was when the screen loaded. Using OneTime not need to be specified. Like OneWay binding, OneTime binding binding, a series of read-only controls was bound to the data and, sends data from the source to the target; however, it does this only as the user interacted with the form and the data source’s values when the application is started or when the DataContext changes were changed, the bound controls remained unchanged. This proand, as a result, does not listen for change notifications in the source. vided a way for the users to compare the changes that were made. Unlike OneWay and OneTime binding, OneWayToSource binding Additionally, OneTime binding is a good choice when your source sends data from the target to the source. Finally, TwoWay binding doesn’t implement INotifyPropertyChanged. sends the source data to the target, and if there are changes in the target property’s value, those will be sent back to the source. A Time to Bind In the previous example, I used OneWay binding because I In the previous example, the TextBox allows TwoWay binding to wanted the source (the selected ListBoxItem) to be sent to the Text- the selected ListBoxItem in the ListBox. This flow of data from Block whenever a change is made in the ListBox selection. I do not the TextBox back to the ListBox happens when the TextBox loswant changes from the TextBlock to go back to the ListBox. Of es focus. To change the event that causes the data to be sent back course, there is no way for a user to edit a to the source, you can specify a value TextBlock. If I want to explore TwoWay Figure 4 XmlDataProvider for UpdateSourceTrigger, which is the binding, I can add a TextBox to this code, binding property that defines when the bind its text and background color to the source should be updated. There are ListBox, and set the Mode to TwoWay. three values that can be set for the Up When a user selects a color in the ListBox, dateSourceTrigger: Explicit, LostFocus, the color is shown in the TextBox and its and PropertyChanged. background color changes. When that When you set the UpdateSourceTrigger user types in a color (such as Cyan) in the to Explicit, the source will not be updat TextBox, the name of the color is updated ed unless the BindingExpression.Update in the ListBox (target to source) and in Source method is called from code. The turn, since the ListBox was updated, the LostFocus setting (the default value for new value is sent to all elements that are the TextBox control) indicates that the bound to the ListBox’s SelectedItem property. This means that the source will be updated when the target control loses focus. The TextBlock will also have its color updated and its text value set to PropertyChanged value indicates that the target will update the the new color (see Figure 3). source every time the target control’s bound property changes. Here’s the code I used to bind the TextBlock (OneWay) and the This setting is useful when you want to dictate when the bindTextBox (TwoWay) to the ListBox: ing will occur. Binding to XML Binding to data sources such as XML and objects is also handy. Figure 4 shows a sample of an XmlDataProvider that contains an embedded list of colors that will be used as a data source. The XmlDataProvider can be used to bind to an XML document or fragment that is either embedded in the XmlDataProvider tag or is in a file referred to in an external location. Embedded XML content must be placed within a tag inside of an XmlDataProvider, as shown in Figure 4. The XmlDataProvider must be given an x:Key value so that it can be referred to by data-binding targets. Notice that the XPath attribute is set to If I change the TwoWay mode back to OneWay, the user will be able to edit the color in the TextBox without causing the changed value to be sent back to the ListBox. Selecting the appropriate binding mode is important. I of30 msdnmagazine Data Points
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.