Before binding XML data with WPF controls, let me give you an overview of the
System.Xml.Linq namespace. It contains the classes for LINQ to XML. LINQ to XML is an in-memory XML programming interface that enables you to modify XML documents efficiently and easily.
Using LINQ to XML, you can:
- Load XML from files or streams.
- Serialize XML to files or streams.
- Create XML trees from scratch using functional construction.
- Query XML trees using LINQ queries.
- Manipulate in-memory XML trees.
- Validate XML trees using XSD.
- Use a combination of these features to transform XML trees from one shape to another.
For data binding efforts, we need to focus on two classes in this namespace, namely:
XElement. The dynamic properties of these classes help us to exactly bind the control to the respective elements in the XML data.
XAttribute class is derived from the
XObject class and it represents an XML attribute. Attributes are not derived from
XNode; they are not nodes in the XML tree. Instead, they are simply name/value pairs associated with an element.
XElement class is derived from the
XContainer class which derives from the
XNode class, and it represents an XML element, the fundamental XML construct. An element has an
XName, optionally one or more attributes, and can optionally contain contents like
XElement contains a list of attributes for that element. Attributes must have a qualified name that is unique to the element.
As an example, the following C# code shows the common task of creating an element that contains an attribute:
XElement phone = new XElement("Phone", new XAttribute("Type", "Home"), "555-555-5555"); Console.WriteLine(phone);
Dynamic Data Binding in WPF and Dynamic Properties
Having learnt that we have a couple of XLinq classes available with us, the next step is to understand how data binding can be performed with these classes.
By default, data binding occurs only when the target UI element is initialized. This is called one-time binding. For most purposes, this is insufficient; with two-way binding, changes to the source are automatically propagated to the target, and changes to the target are automatically propagated to the source.
For one-way or two-way binding to occur, the source must implement a change notification mechanism, for example, by implementing the
INotifyPropertyChanged interface or by using a
PropertyNameChanged pattern for each property supported.
Most LINQ to XML classes do not qualify as proper WPF dynamic data sources. To support WPF data binding, LINQ to XML exposes a set of dynamic properties. These dynamic properties are special run-time properties in the
XElement classes to act as dynamic data sources for WPF and implement change notifications.
The dynamic properties in the
XElement classes cannot be accessed like standard properties. In C#, dynamic properties can only be accessed at run time through facilities provided by the
System.ComponentModel namespace. However, in an XML source, dynamic properties can be accessed through a straightforward notation in the following form: object.dynamic-property.
The dynamic properties for these two classes either resolve to a value that can be used directly, or to an indexer such as Elements and Descendants property of XElement, that must be supplied with an index to obtain the resulting value or collection of values.
To implement WPF dynamic binding, dynamic properties will be used with facilities provided by the
System.Windows.Data namespace, most notably the
XML as Resource Data in XAML
Now, let us look at the various elements of XAML that can be used to define and use XML data. Also, this section explains the elements required for defining the resources, templates, and subsequent binding of these to the WPF controls.
Windows.Resources tag, we can declare the data source to XML data using the
<ObjectDataProvider> and also a
<DataTemplate> that follows a certain pattern of data elements.
<ObjectDataProvider> tag declares an instance of
ObjectDataProvider class, named
LoadedBooks that uses an
XElement as the source. This
XElement is initialized by parsing an embedded XML document (a
CDATA element). Also, white space is preserved when declaring the embedded XML document and when it is parsed. The reason is that the
TextBlock control, which is used to display the raw XML, has no special XML formatting capabilities.
A DataTemplate named “
BookTemplate” is defined to display the entries in the respective controls in the Book List UI section. It uses data binding and LINQ to XML dynamic properties to retrieve the book ID and book name through the following assignments: