Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Gauge for SharePoint
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » Articles » XML Data Binding using Data Templates in XAML

XML Data Binding using Data Templates in XAML

This article describes how to bind XML data using a data template in XAML. For ease of demonstration, the XML is defined as a resource within the XAML document.

Page Views : 7295
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Nevron Gauge for SharePoint
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Overview

Avalon provides a number of conveniences in terms of accessing and binding external data sources.  This tutorial shows how you can bind and display XML data using a data template in XAML.  This technique is also known as creating an XML data island.

XML Data Binding

For this example we will create a very simple XAML application that displays a list of favorites (URLs) from an XML data source.  In our XAML document we will define a data source as a resource and supply that resource with our XML data, in this case a list of URLs.  To do this we will use the XMLDataProvider tag.

<!-- define the XML data source as a resource -->
<XmlDataProvider x:Key=
"BookmarkData" XPath="/Favorites">
          <x:XData>
                   <Favorites xmlns=
"">
                             <Bookmark>
                                      <Title>
Google</Title>
                                      <URL>http://www.google.com</URL>
                             </Bookmark>
                             <Bookmark
>
                                      <Title>
Amazon</Title>
                                      <URL>http://www.amazon.com</URL>
                             </Bookmark>
                             <Bookmark
>
                                      <Title>
Slashdot</Title>
                                      <URL>http://www.slashdot.com</URL>
                             </Bookmark>
                             <Bookmark
>
                                      <Title>
Ars Technica</Title>
                                      <URL>http://www.arstechnica.com</URL>
                             </Bookmark>
                             <Bookmark
>
                                      <Title>
New Egg</Title>
                                      <URL>http://www.newegg.com</URL>
                             </Bookmark>
                   </Favorites
>
          </x:XData
>
</XmlDataProvider>
 

As you can see, we have also defined an XPath attribute within the XMLDataProvider tag.  This path definition allows us to access the XML data based on the parent node "Favorites".  Once this has been defined, we need to prepare our bookmark data for display by binding it to a data template.  We can do this using the DataTemplate and Binding tags, also within the resources portion of our XAML document.

<!-- create a data template to display the desired XML node values -->
<
DataTemplate x:Key
="BookmarkDataTemplate">
        <StackPanel Margin
="5">
                <TextBlock FontSize="12" FontWeight="Bold" Foreground
="White">
                        <TextBlock.Text
>
                                <Binding XPath
="Title"/>
                        </TextBlock.Text
>
                </TextBlock
>
                <TextBlock FontSize="10" Foreground
="LightGray">
                        <TextBlock.Text
>
                                <Binding XPath
="URL"/>
                        </TextBlock.Text
>
                </TextBlock
>
        </StackPanel
>
</
DataTemplate
>

A data template is now configured to accept both Title and URL node values, displaying each in its own TextBlock.  For purposes of layout, we have arranged these in a single StackPanel.  The Binding tag and XPath attribute have been used to define the specific XML nodes we are interested in pulling data from.

All that is left is to use the data template to actually write our data to the screen.  We do this by referencing the data template in the main portion of our XAML document.  Here we will bind the data to a standard ListBox control using the ItemsSource and ItemTemplate properties. 

<!-- write the data to the screen by binding the data template to a list box -->
<
StackPanel
>
        <TextBlock FontSize="14" FontWeight="Bold" Margin="10">My Favorites</TextBlock
>
        <
ListBox
                Background
="#999"
                BorderThickness
="2"
                BorderBrush="White"
 
                Margin
="10"
                ItemsSource
="{Binding Source={StaticResource BookmarkData}, XPath=Bookmark}"
                ItemTemplate
="{StaticResource BookmarkDataTemplate}"/>
</
StackPanel> 

The ItemsSource property specifies a binding source referencing the original XML data we defined in the resources portion of our document.  The XPath property is set to "Bookmark", the repeating child node within "Favorites".  Lastly, the value for ItemTemplate is set to refer to the key of our data template, "BookmarkDataTemplate".

Displayed in its entirity, our XAML file now looks like the following:

<Window x:Class="WindowsApplication1.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="WindowsApplication1" Width="550" Height="400" Name="Form1">

          <Window.Resources>

                    <XmlDataProvider x:Key="BookmarkData" XPath="/Favorites">

                             <x:XData>

                                       <Favorites xmlns="">

                                                <Bookmark>

                                                          <Title>Google</Title>

                                                          <URL>http://www.google.com</URL>

                                                </Bookmark>

                                                <Bookmark>

                                                          <Title>Amazon</Title>

                                                          <URL>http://www.amazon.com</URL>

                                                </Bookmark>

                                                <Bookmark>

                                                          <Title>Slashdot</Title>

                                                          <URL>http://www.slashdot.com</URL>

                                                </Bookmark>

                                                <Bookmark>

                                                          <Title>Ars Technica</Title>

                                                          <URL>http://www.arstechnica.com</URL>

                                                </Bookmark>

                                                <Bookmark>

                                                         <Title>New Egg</Title>

                                                         <URL>http://www.newegg.com</URL>

                                                </Bookmark>

                                      </Favorites>

                             </x:XData>

                   </XmlDataProvider>

 

                   <!-- create a data template to display the desired XML node values -->

                   <DataTemplate x:Key="BookmarkDataTemplate">

                             <StackPanel Margin="5">

                                      <TextBlock FontSize="12" FontWeight="Bold" Foreground="White">

                                                <TextBlock.Text>

                                                         <Binding XPath="Title"/>

                                                </TextBlock.Text>

                                      </TextBlock>

                                      <TextBlock FontSize="10" Foreground="LightGray">

                                                <TextBlock.Text>

                                                         <Binding XPath="URL"/>

                                                 </TextBlock.Text>

                                      </TextBlock>

                             </StackPanel>

                   </DataTemplate> 

</Window.Resources>

 

 

<!-- write the data to the screen by binding the data template to a list box -->

<StackPanel>

          <TextBlock FontSize="14" FontWeight="Bold" Margin="10">My Favorites</TextBlock>

          <ListBox

               Background="#999"

               BorderThickness="2"

               BorderBrush="White"

               Margin="10"

               ItemsSource="{Binding Source={StaticResource BookmarkData}, XPath=Bookmark}"

               ItemTemplate="{StaticResource BookmarkDataTemplate}"/>

</StackPanel> 

</Window>


When compiled, a window is displayed with a standard ListBox populated with our XML data:

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Author
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.