WPF 使用XML作为绑定源时Xaml注意事项

直接在xaml定义时xml时应该注意的!

xml数据

 xml version="1.0" encoding="utf-8"?>
  <StringList >
                    <Srtring Id="1">
                        <Name>AName>
                        <Age>10Age>
                        <Nub>001Nub>
                    Srtring>
                    <Srtring Id="2">
                        <Name>BName>
                        <Age>20Age>
                        <Nub>002Nub>
                    Srtring>
                    <Srtring Id="3">
                        <Name>CName>
                        <Age>30Age>
                        <Nub>003Nub>
                    Srtring>
                    <Srtring Id="4">
                        <Name>DName>
                        <Age>40Age>
                        <Nub>004Nub>
                    Srtring>
                    <Srtring Id="5">
                        <Name>AName>
                        <Age>50Age>
                        <Nub>005Nub>
                    Srtring>
                StringList>

 

如果直接在Xaml页面中定义则是:

使用XmlDataProvider,并设置key,以及Xpath的源,xpath则是xml在被绑定时起始数据的起点,但不包括自身(不设置也可以,但是需要在设置绑定时设置)

也就是xpath设定为StringList,绑定时的起点则是String

且注意的是:XAML页面中不可以包含字样,会报错。还有需要在xml根部加上xmlns=""

例子:

   <Window.Resources>
        <XmlDataProvider   x:Key="xmlData"  >
            <x:XData>
                <StringList xmlns="">
                    <String Id="1">
                        <Name>AName>
                        <Age>10Age>
                        <Nub>001Nub>
                    String>
                    <String Id="2">
                        <Name>BName>
                        <Age>20Age>
                        <Nub>002Nub>
                    String>
                    <String Id="3">
                        <Name>CName>
                        <Age>30Age>
                        <Nub>003Nub>
                    String>
                    <String Id="4">
                        <Name>DName>
                        <Age>40Age>
                        <Nub>004Nub>
                    String>
                    <String Id="5">
                        <Name>AName>
                        <Age>50Age>
                        <Nub>005Nub>
                    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路径正确

例如

 <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>

 

截图

WPF 使用XML作为绑定源时Xaml注意事项_第1张图片

 

你可能感兴趣的:(WPF 使用XML作为绑定源时Xaml注意事项)