Export (0) Print
Expand All

How to: Create Dynamic XAML with LINQ to XML

Silverlight

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="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://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):

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

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.


            // 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='http://schemas.microsoft.com/client/2007' 
        xmlns:x='http://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 = "http://schemas.microsoft.com/client/2007";
            XElement textBlock2 = new XElement(xmlns + "TextBlock",
                new XAttribute(XNamespace.Xmlns + "x", "http://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);



Concepts

Other Resources

Community Additions

ADD
Show:
© 2014 Microsoft