原文:整理:WPF中XmlDataProvider的用法总结

一、目的:了解XmlDataProvider中绑定数据的方法

二、绑定方式主要有三种:

1、Xaml资源中内置:


  1. <!--XPath指定一组节点-->
  2. <XmlDataProvider x:Key="ds1" XPath="XUnits">
  3. <x:XData>
  4. <XUnits xmlns="">
  5. <FFmpegCommandTextParameter Text="设置纪录时间" ToolTip="hh:mm:ss[.xxx]格式的记录时间也支持" Command="-t" Parameter="2001"/>
  6. <FFmpegCommandTextParameter Text="搜索到指定的时间" ToolTip="[-]hh:mm:ss[.xxx]的格式也支持" Command="-ss" Parameter="2001"/>
  7. <FFmpegCommandTextParameter Text="设置标题" ToolTip="设置标题" Command="-title" Parameter="2001"/>
  8. <FFmpegCommandTextParameter Text="设置作者" ToolTip="设置作者" Command="-author" Parameter="2001"/>
  9. <FFmpegCommandTextParameter Text="设置版权" ToolTip="设置版权" Command="-copyright" Parameter="2001"/>
  10. <FFmpegCommandTextParameter Text="设置评论" ToolTip="设置评论" Command="-comment" Parameter="2001"/>
  11. <FFmpegCommandCheckParameter Text="激活高质量设置" ToolTip="激活高质量设置" Command="-hq" IsChecked="false"/>
  12. <FFmpegCommandTextParameter Text="设置目标文件类型" ToolTip="设置目标文件类型" Command="-author" Parameter="2001"/>
  13. <FFmpegCommandCheckParameter Text="激活高质量设置" ToolTip="激活高质量设置" Command="-hq" IsChecked="false"/>
  14. </XUnits>
  15. </x:XData>
  16. </XmlDataProvider>

2、Source属性绑定外部xml文件

  <XmlDataProvider Source="myfile.xml" x:Key="mykey" XPath="/Root"/>

3、绑定代码XmlDocument实例

<XmlDataProvider x:Key="InventoryData" XPath="Inventory/Books" Document="{Binding MyDocument}"/>

三、绑定到控件并设置模板

1、绑定树形结构


  1. <!--年级模版-->
  2. <HierarchicalDataTemplate DataType="Grade" ItemsSource="{Binding XPath=Class}">
  3. <TextBlock Text="{Binding XPath=@Name}"></TextBlock>
  4. </HierarchicalDataTemplate>
  5. <!--班级模版-->
  6. <HierarchicalDataTemplate DataType="Class" ItemsSource="{Binding XPath=Group}">
  7. <RadioButton Content="{Binding XPath=@Name}"></RadioButton>
  8. </HierarchicalDataTemplate>
  9. <!--分组模版-->
  10. <HierarchicalDataTemplate DataType="Group">
  11. <CheckBox Content="{Binding XPath=@Name}"></CheckBox>
  12. </HierarchicalDataTemplate>
  13. <!--数据模版-->
  14. <XmlDataProvider x:Key="ds" XPath="Data/Grade">
  15. <x:XData>
  16. <Data xmlns="">
  17. <Grade Name="一年级">
  18. <Class Name="甲班">
  19. <Group Name="A组"></Group>
  20. <Group Name="B组"></Group>
  21. <Group Name="C组"></Group>
  22. </Class>
  23. <Class Name="乙班">
  24. <Group Name="A组"></Group>
  25. <Group Name="B组"></Group>
  26. <Group Name="C组"></Group>
  27. </Class>
  28. </Grade>
  29. <Grade Name="二年级">
  30. <Class Name="丙班">
  31. <Group Name="A组"></Group>
  32. <Group Name="B组"></Group>
  33. <Group Name="C组"></Group>
  34. </Class>
  35. <Class Name="丁班">
  36. <Group Name="A组"></Group>
  37. <Group Name="B组"></Group>
  38. <Group Name="C组"></Group>
  39. </Class>
  40. </Grade>
  41. </Data>
  42. </x:XData>
  43. </XmlDataProvider>
  44. <Menu ItemsSource="{Binding Source={StaticResource ds}}"></Menu>
  45. <TreeView ItemsSource="{Binding Source={StaticResource ds}}" Margin="5"></TreeView>

2、绑定到ListBox、Comboboxl并设置模板


  1. <!--Xml中的元素名可以作为DataType-->
  2. <DataTemplate DataType="XUnit">
  3. <Grid>
  4. <StackPanel Orientation="Horizontal">
  5. <Grid>
  6. <Rectangle Fill="Red" Width="{Binding XPath=@Price}" Stroke="Yellow"></Rectangle>
  7. <TextBlock Text="{Binding XPath=@Year}"/>
  8. </Grid>
  9. <TextBlock Text="{Binding XPath=@Price}"></TextBlock>
  10. </StackPanel>
  11. </Grid>
  12. </DataTemplate>
  13. <!--XPath指定一组节点-->
  14. <XmlDataProvider x:Key="ds1" XPath="XUnits/XUnit">
  15. <x:XData>
  16. <XUnits xmlns="">
  17. <XUnit Price="100" Year="2001"></XUnit>
  18. <XUnit Price="120" Year="2002"></XUnit>
  19. <XUnit Price="140" Year="2003"></XUnit>
  20. <XUnit Price="180" Year="2004"></XUnit>
  21. <XUnit Price="150" Year="2005"></XUnit>
  22. <XUnit Price="200" Year="2006"></XUnit>
  23. </XUnits>
  24. </x:XData>
  25. </XmlDataProvider>

四、设置XPath

1、直接设置类型的XPath对应的属性


  1. <DataTemplate DataType="FFmpegCommandTextParameter">
  2. <Grid Width="{Binding RelativeSource={RelativeSource AncestorType=ListBox},Path=ActualWidth}">
  3. <Grid>
  4. <Grid.ColumnDefinitions>
  5. <ColumnDefinition/>
  6. <ColumnDefinition Width="2*"/>
  7. </Grid.ColumnDefinitions>
  8. <TextBlock Text="{Binding XPath=@Text}" ToolTip="{Binding XPath=@ToolTip}"/>
  9. <TextBox Text="{Binding XPath=@Parameter}" ToolTip="{Binding XPath=@Command}" Grid.Column="1"/>
  10. </Grid>
  11. </Grid>
  12. </DataTemplate>

如:绑定FFmpegCommandTextParameter节点下面Text属性,注意:@转义字符必须添加

2、在 XmlDataProvider中设置根节点XPath

<XmlDataProvider Source="myfile.xml" x:Key="myfile" XPath="/Root"/>

3、筛选

<ListBox DataContext="{Binding Source={StaticResource SourceKey},XPath=/Root/Item[@ID/=1]}"/>
<ListBox DataContext="{Binding Source={StaticResource SourceKey},XPath=/Root/Item[4]}"/>
<ListBox DataContext="{Binding Source={StaticResource SourceKey},XPath=/Root/Item[@ID&gt;2]}" />

  1. <ListBox.ItemsSource>
  2. <Binding Source="{StaticResource InventoryData}"
  3. XPath="*[@Stock='out'] | *[@Number>=8 or @Number=3]"/>
  4. </ListBox.ItemsSource>
  • XPath="Book[1]" 将返回第一个 Book 元素(“XML in Action”)。 请注意 XPath 索引从 1 而不是从 0 开始

  • XPath="Book[@*]" 将返回带有任意特性的所有 Book 元素。

  • XPath="Book[last()-1]" 将返回第二个至最后一个 Book 元素(“Introducing Microsoft .NET”)。

  • XPath="*[position()>3]" 将返回除前 3 个元素之外的所有 Book 元素。

4、匹配所有类型


  1. <ListBox ScrollViewer.HorizontalScrollBarVisibility="Hidden">
  2. <ListBox.ItemsSource>
  3. <Binding Source="{StaticResource ds1}" XPath="*"/>
  4. </ListBox.ItemsSource>
  5. </ListBox>

五、数据同步到XML


  1. XmlDataProvider xml = this.Resources["xmlData"] as XmlDataProvider;
  2. xml.Document.Save("D:/temp.xml");

六、绑定到TextBlock

数据源一:


  1. <XmlDataProvider x:Key="ss" >
  2. <x:XData>
  3. <curriculum_info xmlns="" class_name = "高等数学" teacher = "tttt" place ="24楼301室" time="31">34353</curriculum_info>
  4. </x:XData>
  5. </XmlDataProvider>

绑定到属性:

<TextBlock  Text="{Binding Source={StaticResource ss}, XPath=curriculum_info/@class_name}"/>

绑定到节点:

<TextBlock  Text="{Binding Source={StaticResource ss}, XPath=curriculum_info}"/>

数据源二:


  1. <XmlDataProvider x:Key="ss" XPath="curriculum_info">
  2. <x:XData>
  3. <curriculum_info xmlns="" class_name = "高等数学" teacher = "吕良福" place ="24楼301室" time="31">34353</curriculum_info>
  4. </x:XData>
  5. </XmlDataProvider>

绑定到属性:

<TextBlock  Text="{Binding Source={StaticResource ss}, XPath=@class_name}"/>

数据源三:


  1. <XmlDataProvider x:Key="ss" XPath="List">
  2. <x:XData>
  3. <List xmlns="">
  4. <curriculum_info xmlns="" class_name = "高等数学" teacher = "ttt" place ="24楼301室" time="31">34353</curriculum_info>
  5. </List>
  6. </x:XData>
  7. </XmlDataProvider>

<TextBlock  Text="{Binding Source={StaticResource ss},XPath=curriculum_info}"/>

注意:xmlns=""必须含有(重要)

整理:WPF中XmlDataProvider的用法总结的更多相关文章

  1. 整理:WPF中CommandBindings的用法

    原文:整理:WPF中CommandBindings的用法 目的:了解一下CommandBindings.InputBindings.ICommandSource中在WPF中扮演什么样的角色 Comma ...

  2. WPF中StringFormat的用法

    原文:WPF中StringFormat的用法 WPF中StringFormat的用法可以参照C#中string.Format的用法 1. C#中用法: 格式化货币(跟系统的环境有关,中文系统默认格式化 ...

  3. WPF中StringFormat的用法--显示特定位数的数字

    原文:WPF中StringFormat的用法--显示特定位数的数字 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/huangli321456/art ...

  4. WPF中log4net的用法

    WPF中如何使用log4nethttp://www.cnblogs.com/C-Sharp2/archive/2013/04/12/WPF-LOG4NET.html Apache log4net Ma ...

  5. wpf中INotifyPropertyChanged的用法

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Linq;using Sy ...

  6. 整理:WPF中Binding的几种写法

    原文:整理:WPF中Binding的几种写法 目的:整理WPF中Bind的写法 <!--绑定到DataContext--> <Button Content="{Bindin ...

  7. 在 WPF 中的线程

    线程处理使程序能够执行并发处理,以便它可以做多个操作一次.节省开发人员从线程处理困难的方式,设计了 WPF (窗口演示文稿基金会).这篇文章可以帮助理解线程在 WPF 中的正确用法. WPF 内部线程 ...

  8. WPF中的常用布局 栈的实现 一个关于素数的神奇性质 C# defualt关键字默认值用法 接口通俗理解 C# Json序列化和反序列化 ASP.NET CORE系列【五】webapi整理以及RESTful风格化

    WPF中的常用布局   一 写在开头1.1 写在开头微软是一家伟大的公司.评价一门技术的好坏得看具体的需求,没有哪门技术是面面俱到地好,应该抛弃对微软和微软的技术的偏见. 1.2 本文内容本文主要内容 ...

  9. 转:WPF中ListBox的创建和多种绑定用法

    先从最容易的开始演示ListBox控件的创建. Adding ListBox Items下面的代码是向ListBox控件中添加多项ListBoxItem集合.XAML代码如下:<ListBox ...

随机推荐

  1. scrapy设置logger日志

    1.在settings中设置log级别,在settings.py中添加一行: LOG_LEVEL = 'WARNING' Scrapy提供5层logging级别: CRITICAL - 严重错误 ER ...

  2. ajax请求体

    jquery向服务器发送一个ajax请求后,可以返回多种类型的数据格式,包括:html,xml,json,text等. $.ajax({ url:"http://www.test.com&q ...

  3. mac运行模拟器simulator突然很慢

    一直都正常,突然变慢,而且慢的离谱. 上网查了下,这里记录下,或许问题不仅限于此. simulator->Debug->Slow Animations. 这个Slow Animations ...

  4. 解决nginx反向代理webservice的soap:address location问题

    原文:https://blog.csdn.net/mn960mn/article/details/50716768 一:首先来发布一个web service package com.ws.servic ...

  5. docker学习10-注册docker hub账号

    前言 Docker Hub是Docker的远程镜像仓库,是 docker 官网推出的 docker 仓库的一个公共服务器,在上面可以有私有和公有的镜像. 类似于 github,可以上传自己镜像文件,也 ...

  6. CodeForces 150E: Freezing with Style

    题目传送门:CF150E. 据说这个傻逼题还有一个 \(\log\) 的做法,但是我还不会. 题意简述: 给定一棵 \(n\)(\(2\le n\le 10^5\))个点的树,边有边权. 定义一条路径 ...

  7. CentOS7.5 上使用 bundle 文件安装 MySQL8.0 MySQL5.0

    CentOS7.5 上使用 bundle 文件安装 MySQL8.0 MySQL5.0 CentOS7.5 环境 [root@instance-fjii60o3 ~]# rpm -qi centos- ...

  8. JS 中的 new 操作符

    按照javascript语言精粹中所说,如果在一个函数前面带上new来调用该函数,那么将创建一个隐藏连接到该函数的prototype成员的新对象,同时this将被绑定到那个新对象上.这个话很抽象,我想 ...

  9. hdu1873-看病要排队-(结构体优先队列)

    http://acm.hdu.edu.cn/showproblem.php?pid=1873 #include<stdio.h> #include<iostream> #inc ...

  10. (二)STM32开发例程

    1控制LED和继电器 除了 PA11和PA12其他都可以 void setup() { pinMode(PC13, OUTPUT); pinMode(PC14, OUTPUT); pinMode(PC ...