September 04, 2006

WPF- Data Binding

Data binding

  • Data templates are used to specify how a control must display an object.

  • Data templates act as a bridge between objects and the user interface, and give designers great flexibility to manage data presentation.

Example:

This example shows how to bind XML data island with text.

<Page x:Class="TestXBAP.Page1"

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

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

Title="Page1" Height="170.4" Width="354.4">

<Page.Resources>

<XmlDataProvider x:Key="EmployeeData" XPath="/Employees/Employee">

<x:XData>

<!-- xml data island -->

<Employees xmlns="">

<Employee Name="Bill Gates" DOB="13-Jul-1973">

<Title>CEO</Title>

</Employee>

</Employees>

</x:XData>

</XmlDataProvider>

</Page.Resources>

<!-- binding to data island -->

<StackPanel DataContext="{StaticResource EmployeeData}">

<TextBlock Text="{Binding XPath=@Name}" FontSize="20" />

<TextBlock Text="{Binding XPath=@DOB}" FontSize="10" />

<TextBlock Text="{Binding XPath=Title/text()}" FontSize="10" />

</StackPanel>

</Page>