WPF 使用XML作为绑定源时Xaml注意事项
2021-03-07 10:27
标签:color mss idv grid 路径 apr idt mem div
直接在xaml定义时xml时应该注意的! xml数据 如果直接在Xaml页面中定义则是: 使用XmlDataProvider,并设置key,以及Xpath的源,xpath则是xml在被绑定时起始数据的起点,但不包括自身(不设置也可以,但是需要在设置绑定时设置) 也就是xpath设定为StringList,绑定时的起点则是String 且注意的是:XAML页面中不可以包含字样,会报错。还有需要在xml根部加上xmlns="" 例子: 如果想要获取属性则是需要使用@,并且确保xpath路径正确 例如 截图 WPF 使用XML作为绑定源时Xaml注意事项 标签:color mss idv grid 路径 apr idt mem div 原文地址:https://www.cnblogs.com/lonelyxmas/p/12833938.html 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>
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>
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注意事项
文章链接:http://soscw.com/index.php/essay/61294.html