How to: Create Dynamic XAML with LINQ to XML

Microsoft Silverlight will reach end of support after October 2021. Learn more.

This topic shows how to dynamically create TextBlock controls with LINQ to XML.

To configure a Silverlight Visual Studio project to run this example

  1. In Solution Explorer, add assembly reference to the System.Xml.Linq.dll.

  2. Modify your page.xaml file so it has the following content:

    <UserControl x:Class="SilverlightApplication1.Page"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" 
        Width="400" Height="300">
        <Canvas x:Name="LayoutRoot" Background="White">
        </Canvas>
    </UserControl>
    
  3. In the page.xaml.cs (page.xaml.vb in Visual Basic) source file for your application, add the following using statements (Imports in Visual Basic):

    Imports System.Xml.Resolvers
    Imports System.Xml
    Imports System.Xml.Linq
    Imports System.IO
    Imports System.Text
    Imports System.Windows.Markup
    
    using System.Windows.Markup;
    using System.Xml.Resolvers;
    using System.Xml;
    using System.Xml.Linq;
    using System.IO;
    using System.Text;
    

Example

The code example creates an XElement object and populates it with contact information. It also dynamically creates two TextBlock controls. The content of the contact object is then added to the Text attribute of TextBlock controls and displayed on the page.

Dim contacts = <Contacts>
                   <Contact1>
                       <Name>Patrick Hines</Name>
                       <Phone>206-555-0144</Phone>
                       <Address>
                           <Street1>123 Main St</Street1>
                           <City>Mercer Island</City>
                           <State>WA</State>
                           <Postal>68042</Postal>
                       </Address>
                   </Contact1>
                   <Contact2>
                       <Name>Yoshi Latime</Name>
                       <Phone>503-555-6874</Phone>
                       <Address>
                           <Street1>City Center Plaza 516 Main St.</Street1>
                           <City>Elgin</City>
                           <State>OR</State>
                           <Postal>97827</Postal>
                       </Address>
                   </Contact2>
               </Contacts>

' Create the first TextBlock control.
' Note that the element has to declare two XAML namespaces.
Dim textBlock1 = <TextBlock
                     xmlns='https://schemas.microsoft.com/client/2007'
                     xmlns:x='https://schemas.microsoft.com/winfx/2006/xaml'
                     TextWrapping='Wrap'
                     Width='400'
                     Canvas.Top='10'
                     Text=''/>

' Get the first child element from contacts xml tree.
Dim contact1 As XElement = contacts.Element("Contact1")

' Set the value of the "Text" attribute 
' to the content of the first contact in the contacts xml tree.
textBlock1.Attribute("Text").SetValue(contact1.ToString())


' Get the second child element from contacts xml tree.
Dim contact2 As XElement = contacts.Element("Contact2")

' Create the second TextBlock control.
' Note that the element has to declare two XAML namespaces.
Dim textBlock2 = <TextBlock
                     xmlns='https://schemas.microsoft.com/client/2007'
                     xmlns:x='https://schemas.microsoft.com/winfx/2006/xaml'
                     Width='600'
                     Canvas.Top='250'
                     Text=<%= contact2.ToString() %>/>

' Add TextBlock control to the page
LayoutRoot.Children.Add(CType(XamlReader.Load(textBlock1.ToString()), UIElement))
' Add TextBlock control to the page
LayoutRoot.Children.Add(CType(XamlReader.Load(textBlock2.ToString()), UIElement))
            // Populate XElement contacts with Contacts information.  
            XElement contacts =
                new XElement("Contacts",
                    new XElement("Contact1",
                        new XElement("Name", "Patrick Hines"),
                        new XElement("Phone", "206-555-0144"),
                        new XElement("Address",
                            new XElement("Street1", "123 Main St"),
                            new XElement("City", "Mercer Island"),
                            new XElement("State", "WA"),
                            new XElement("Postal", "68042")
                        )
                    ),
                    new XElement("Contact2",
                        new XElement("Name", "Yoshi Latime"),
                        new XElement("Phone", "503-555-6874"),
                        new XElement("Address",
                            new XElement("Street1", "City Center Plaza 516 Main St."),
                            new XElement("City", "Elgin"),
                            new XElement("State", "OR"),
                            new XElement("Postal", "97827")
                        )
                    )
                );

            // Create the first TextBlock control.
            // Note that the element has to declare two XAML namespaces.
            XElement textBlock1 = XElement.Parse(
                    @"<TextBlock 
        xmlns='https://schemas.microsoft.com/client/2007' 
        xmlns:x='https://schemas.microsoft.com/winfx/2006/xaml' 
        TextWrapping= 'Wrap'
        Width = '400'
        Canvas.Top = '10'
        Text=''/>");

            // Get the first child element from contacts xml tree.
            XElement contact1 = contacts.Element("Contact1");

            // Set the value of the last attribute "Text"
            // to the content of contacts xml tree.
            textBlock1.LastAttribute.SetValue(contact1.ToString());


            // Get the second child element from contacts xml tree.
            XElement contact2 = contacts.Element("Contact2");

            // Create the second TextBlock control.
            // Note that the element has to declare two XAML namespaces.
            XNamespace xmlns = "https://schemas.microsoft.com/client/2007";
            XElement textBlock2 = new XElement(xmlns + "TextBlock",
                new XAttribute(XNamespace.Xmlns + "x", "https://schemas.microsoft.com/winfx/2006/xaml"),
                new XAttribute("Canvas.Top", 250),
                new XAttribute("Width", "600"),
                new XAttribute("Text", contact2.ToString())
                );


            // Add TextBlock control to the page
            LayoutRoot.Children.Add(XamlReader.Load(textBlock1.ToString()) as UIElement);
            // Add TextBlock control to the page
            LayoutRoot.Children.Add(XamlReader.Load(textBlock2.ToString()) as UIElement);

See Also

Concepts

Other Resources