XmlDataProvider的简单应用

时间:2024-03-08 12:08:41

1.Xml文档:



代码
1 <?xml version="1.0" encoding="utf-8" ?>
2  <Root>
3 <Item ID="1" Data="意大利风格"/>
4 <Item ID="2" Data="挪威风格"/>
5 <Item ID="3" Data="美国风格"/>
6 <Item ID="4" Data="非洲风格">
7 <SubItem ID="41" Data="埃及风格"></SubItem>
8 </Item>
9  </Root>

 

2.Xaml

 

代码
<Window x:Class="SdkXamlBrowser.Window1"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
Title
="Window1" Height="358" Width="709">
<Window.Resources>
<XmlDataProvider Source="a.xml" x:Key="aa" XPath="/Root"/>
</Window.Resources>
<Grid>
<TextBlock Text="{Binding ElementName=lb,Path=SelectedValue}" />
<StackPanel Orientation="Horizontal" Margin="20,40,342,0">
<!-- DisplayMemberPath="@Data" 必须加【@】 -->
<!--筛选:ID==1 "="必须转义,改为"\="-->
<ListBox DataContext="{Binding Source={StaticResource aa},XPath=/Root/Item[@ID\=1]}" Width="100" Name="lb"
ItemsSource
="{Binding}" DisplayMemberPath="@Data" SelectedValuePath="@ID">
</ListBox>
<ListBox DataContext="{Binding Source={StaticResource aa},XPath=/Root/Item[4]}" Name="lb1" Width="100"
ItemsSource
="{Binding}" DisplayMemberPath="@Data" SelectedValuePath="@ID">
</ListBox>
<!--筛选:ID大于2 ">"必须转义,改为"&gt;"-->
<ListBox DataContext="{Binding Source={StaticResource aa},XPath=/Root/Item[@ID&gt;2]}" Name="lb2" Width="100"
ItemsSource
="{Binding}" DisplayMemberPath="@Data" SelectedValuePath="@ID">
</ListBox>
</StackPanel>
</Grid>
</Window>