示例
在项目中新建 XML 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| <?xml version="1.0" encoding="utf-8" ?> <Inventory> <Books> <Book ISBN="0-7356-0562-9" Stock="in" Number="9"> <Title>XML in Action</Title> <Summary>XML Web Technology</Summary> </Book> <Book ISBN="0-7356-1370-2" Stock="in" Number="8"> <Title>Programming Microsoft Windows With C#</Title> <Summary>C# Programming using the .NET Framework</Summary> </Book> <Book ISBN="0-7356-1288-9" Stock="out" Number="7"> <Title>Inside C#</Title> <Summary>C# Language Programming</Summary> </Book> <Book ISBN="0-7356-1377-X" Stock="in" Number="5"> <Title>Introducing Microsoft .NET</Title> <Summary>Overview of .NET Technology</Summary> </Book> <Book ISBN="0-7356-1448-2" Stock="out" Number="4"> <Title>Microsoft C# Language Specifications</Title> <Summary>The C# language definition</Summary> </Book> </Books> <CDs> <CD Stock="in" Number="3"> <Title>Classical Collection</Title> <Summary>Classical Music</Summary> </CD> <CD Stock="out" Number="9"> <Title>Jazz Collection</Title> <Summary>Jazz Music</Summary> </CD> </CDs> </Inventory>
|
将 XMLFile1.xml 的生成操作设为资源或内容
示例 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <Grid> <Grid.Resources> <XmlDataProvider x:Key="Data" Source="XMLFile1.xml" XPath="Inventory/CDs" /> </Grid.Resources> <TabControl ItemsSource="{Binding Source={StaticResource Data},XPath=CD}"> <TabControl.ItemTemplate> <DataTemplate> <TextBlock> <TextBlock.Text> <MultiBinding StringFormat="[{0},{1}]"> <Binding XPath="@Number" /> <Binding XPath="@Stock" /> </MultiBinding> </TextBlock.Text> </TextBlock> </DataTemplate> </TabControl.ItemTemplate> <TabControl.ContentTemplate> <DataTemplate> <StackPanel> <TextBlock Text="{Binding XPath=Title,StringFormat=标题:{0}}" /> <TextBlock Text="{Binding XPath=Summary,StringFormat=概括:{0}}" /> </StackPanel> </DataTemplate> </TabControl.ContentTemplate> </TabControl> </Grid>
|
示例 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <Grid> <ListBox ItemsSource="{Binding XPath=Book}"> <ListBox.DataContext> <XmlDataProvider x:Name="Data" Source="XMLFile1.xml" XPath="Inventory/Books" /> </ListBox.DataContext> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock Text="{Binding XPath=Title}"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid>
|