Window1.xaml 前台代码
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPFDataBindingXML" Height="372" Width="462">
<Window.Resources>
<XmlDataProvider x:Key="guestSource" Source="guest.xml" XPath="/Guests/*"/>
<DataTemplate x:Key="showTitle">
<TextBlock Text="{Binding XPath=Title}"/>
</DataTemplate>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontStyle" Value="Italic"/>
<Setter Property="FontFamily" Value="Trebuchet MS"/>
<Setter Property="FontSize" Value="12"/>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ListBox Grid.Row="0" Grid.Column="0" Margin="5,5,5,5" ItemsSource="{Binding Source={StaticResource guestSource}}"
ItemTemplate="{StaticResource showTitle}" IsSynchronizedWithCurrentItem="True">
</ListBox>
<StackPanel Grid.Row="0" Grid.Column="1" Margin="5,5,5,5">
<TextBlock>
Title:
<TextBlock Margin="10,0,0,0" DataContext="{Binding Source={StaticResource guestSource}}" Text="{Binding XPath=Title}"/>
</TextBlock>
<TextBlock>
Coment:
<TextBlock Margin="10,0,0,0" DataContext="{Binding Source={StaticResource guestSource}}" Text="{Binding XPath=Coment}"/>
</TextBlock>
<TextBlock>
Reply:
<TextBlock Margin="10,0,0,0" DataContext="{Binding Source={StaticResource guestSource}}" Text="{Binding XPath=Reply}"/>
</TextBlock>
</StackPanel>
</Grid>
</Window>
******************************************************
guest.xml 数据结构
<?xml version="1.0" encoding="utf-8" ?>
<Guests>
<xs:schema id="Guests" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Guests" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Guest">
<xs:complexType>
<xs:sequence>
<xs:element name="Title" type="xs:string" minOccurs="0" />
<xs:element name="Coment" type="xs:string" minOccurs="0" />
<xs:element name="Reply" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<Guest>
<Title>sohu</Title>
<Coment>job plan</Coment>
<Reply>嘿嘿呵呵</Reply>
</Guest>
<Guest>
<Title>why?</Title>
<Coment>because ?</Coment>
<Reply>嘿嘿?</Reply>
</Guest>
<Guest>
<Title>fet</Title>
<Coment>job plan</Coment>
<Reply>嘿嘿呵呵</Reply>
</Guest>
<Guest>
<Title>goog</Title>
<Coment>job plan</Coment>
<Reply>嘿嘿呵呵</Reply>
</Guest>
<Guest>
<Title>heal</Title>
<Coment>job plan</Coment>
<Reply>嘿嘿呵呵</Reply>
</Guest>
</Guests>