直接在xaml定義時xml時應該注意的!ide
xml數據spa
<?xml version="1.0" encoding="utf-8"?> <StringList > <Srtring Id="1"> <Name>A</Name> <Age>10</Age> <Nub>001</Nub> </Srtring> <Srtring Id="2"> <Name>B</Name> <Age>20</Age> <Nub>002</Nub> </Srtring> <Srtring Id="3"> <Name>C</Name> <Age>30</Age> <Nub>003</Nub> </Srtring> <Srtring Id="4"> <Name>D</Name> <Age>40</Age> <Nub>004</Nub> </Srtring> <Srtring Id="5"> <Name>A</Name> <Age>50</Age> <Nub>005</Nub> </Srtring> </StringList>
若是直接在Xaml頁面中定義則是:code
使用XmlDataProvider,並設置key,以及Xpath的源,xpath則是xml在被綁定時起始數據的起點,但不包括自身(不設置也能夠,可是須要在設置綁定時設置)xml
也就是xpath設定爲StringList,綁定時的起點則是Stringblog
且注意的是:XAML頁面中不能夠包含<?xml version="1.0" ?>字樣,會報錯。還有須要在xml根部加上xmlns=""utf-8
例子:io
<Window.Resources> <XmlDataProvider x:Key="xmlData" > <x:XData> <StringList xmlns=""> <String Id="1"> <Name>A</Name> <Age>10</Age> <Nub>001</Nub> </String> <String Id="2"> <Name>B</Name> <Age>20</Age> <Nub>002</Nub> </String> <String Id="3"> <Name>C</Name> <Age>30</Age> <Nub>003</Nub> </String> <String Id="4"> <Name>D</Name> <Age>40</Age> <Nub>004</Nub> </String> <String Id="5"> <Name>A</Name> <Age>50</Age> <Nub>005</Nub> </String> </StringList> </x:XData> </XmlDataProvider> </Window.Resources> <Grid> <ListView ItemsSource="{Binding Source={StaticResource xmlData},XPath=StringList/*}"> <ListView.View> <GridView> <GridViewColumn Header="Name" Width="60" DisplayMemberBinding="{Binding XPath=Name}" /> <GridViewColumn Header="Age" Width="60" DisplayMemberBinding="{Binding XPath=Age}" /> <GridViewColumn Header="Nub" Width="60" DisplayMemberBinding="{Binding XPath=Nub}" /> </GridView> </ListView.View> </ListView> </Grid>
若是想要獲取屬性則是須要使用@,而且確保xpath路徑正確class
例如coding
<ListView ItemsSource="{Binding Source={StaticResource xmlData},XPath=/StringList/*}"> <ListView.View> <GridView> <GridViewColumn Header="Id" Width="60" DisplayMemberBinding="{Binding XPath=@Id}" /> <GridViewColumn Header="Id" Width="60" DisplayMemberBinding="{Binding XPath=Age}" /> <GridViewColumn Header="Id" Width="60" DisplayMemberBinding="{Binding XPath=Nub}" /> </GridView> </ListView.View> </ListView>
截圖List