We can also bind the XML data with UI elements. Lets take a first step to define the XML data in WPF program. We can define the XML data using XmlDataProvider class. This class is inherited by abstract class named DataSourceProvider. DataSourceProvide is a wrapper on data model and has a property Dispatcher. It means this class can work with WPF threading model. There are two classes inherited by this class named ObjectDataProvider and XmlDataProvider. Here is a class diagram of these classes.
This class can also be used in XAML. Here is a code to use this class in XAML.
1: <XmlDataProvider x:Key="StatesInformation" XPath="States">2: <x:XData>3: <States xmlns="">4: <State Name="Maryland" Capital="Annapolis"/>5: <State Name="California" Capital="Sacramento"/>6: <State Name="Taxes" Capital="Austin"/>7: <State Name="Washington" Capital="Olympia"/>8: </States>9: </x:XData>10: </XmlDataProvider>11:We define this in windows Resource section and use XPath property of data binding class to perform binding. Here is a complete XAML code to demonstrate this.
1: <Window x:Class="xmlDataBinding.Window1"2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"4: Title="XML Data Binding" Height="300" Width="400">5: <Window.Resources>6: <XmlDataProvider x:Key="StatesInformation" XPath="States">7: <x:XData>8: <States xmlns="">9: <State Name="Maryland" Capital="Annapolis"/>10: <State Name="California" Capital="Sacramento"/>11: <State Name="Taxes" Capital="Austin"/>12: <State Name="Washington" Capital="Olympia"/>13: </States>14: </x:XData>15: </XmlDataProvider>16: </Window.Resources>17:18: <ListBox Margin="5" HorizontalContentAlignment="Stretch"19: ItemsSource="{Binding Source={StaticResource StatesInformation},20: XPath=State}">
21: <ListBox.ItemTemplate>22: <DataTemplate>23: <Border Margin="5" BorderThickness="3" BorderBrush="Navy" CornerRadius="5" Background="Beige">24: <StackPanel Margin="5">25: <TextBlock FontWeight="Bold" Text="{Binding XPath=@Name}"/>26: <TextBlock Text="{Binding XPath=@Capital}"/>27: </StackPanel>28: </Border>29: </DataTemplate>30: </ListBox.ItemTemplate>31: </ListBox>32: </Window>33:Here is the output of this program.
great going zee,
you are near to publish a book …
are you using WPF professionally ?
By: Ghaffar on October 18, 2009
at 3:51 pm
Thanks ghaffar to like it. I cant say anything about book right now, but i am not using WPF professionaly. The only thing i am doing is giving traning of WPF to my team in my offic.e
By: zamjad on October 18, 2009
at 4:51 pm