MSDN Magazine - December 2007 - (Page 31) “/colors”.This attribute defines the level of the XML content that will be used as the data source.This becomes very useful when binding to a large XML structure that may be contained in a file or database and the data you want to bind to is not the root element. An XmlDataProvider is a resource that can be placed inside of a context-specific resource.As Figure 4 shows,the XmlDataProvider is defined as a resource within the context of the StackPanel. This means that the XmlDataProvider will be available to all content inside of that StackPanel. Setting the context of a resource helps limit the exposure of a data source to the appropriate areas. This enables you to create well-defined, self-contained regions of both controls and supporting resources within your page,thus improving readability. The syntax for binding to a resource is slightly different than it is for binding to an element.When binding to a control, you set the ElementName and the Path properties of the Binding. However, when you bind to a resource you set the Source and, since we are binding to an XmlDataProvider, you set the XPath property of the Binding as well. For example, the following code will bind the ListBox’s items to the MoreColors resource. The Source property is set to a resource and it is specified as a StaticResource named MoreColors. The XPath property indicates that the items will be bound to the element’s name attribute within the XML data source: jectType designates the object that will provide the data-binding source while the MethodName indicates the method that will be invoked to get the data.For example,assuming I have a class called PersonService that has a method called GetPersonList that returns a List , the ObjectDataProvider would look like this: If you want a more complete look,the PersonService and Person classes, as well as all other sample code, are contained in the code that accompanies this column. There are a handful of other properties that are available on the ObjectDataProvider.The ConstructionParameters property allows you to pass parameters to the constructor of the class being invoked. You can also specify parameters using the MethodParameters property, and you can use the ObjectInstance property to specify an existing instance of an object as the source. If you want the data to be retrieved asynchronously, you can set the IsAsynchronous property of the ObjectDataProvider to true. Then the user will be able to interact with the screen while waiting for the data to populate in the target control that is bound to the ObjectDataProvider’s source. When adding an ObjectDataProvider, you have to qualify the namespace of the data-source class. In this case, I have to add an I specified StaticResource in this case because the XML will not change. If changes occur in the data source, they will not be sent to the target. The DynamicResource setting indicates the opposite—changes will be sent. This is useful when referencing system themes, languages in globalization, or fonts.A DynamicResource will allow these types of settings to be propagated throughout the UI elements that are bound to them dynamically. The XmlDataProvider can also point to an external source for the XML content. For my example, I have a file named colors.xml that contains the list of colors I want my ListBox to be bound to. I can simply add a second XmlDataProvider resource to the StackPanel and direct it to the XML file.Notice I set the Source attribute to the name of the XML file and the x:Key to Colors: Both XmlDataProviders exist as resources within the same StackPanel.I can tell the ListBox to bind itself to this new resource by changing the name that the StaticResource is set to: Object Binding and DataTemplates While the XmlDataProvider works great for XML, when you want to bind to an object or a list of objects, you can create an ObjectDataProvider as a resource.The ObjectDataProvider’s ObData Points december2007 31 http://www.ektron.com/msdn http://www.ektron.com/msdn
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.