原文:整理: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. 深浅拷贝、集合set、函数、日志

    #-----深浅拷贝---- import copy a = ["xiaoming",111,[5000,2000]] b = a print("b:%s" % ...

  2. MySql || 快速创建100w条记录

    平时每个开发者都会讨论数据量大时,sql的优化问题.但是并不是每个人都会有100w的数据量可以用来实战,那么今天我们就自己动手,模拟一个100w数据量的表. 创建原理 其实创建的方法有很多,有快的也有 ...

  3. python 和 R 中的整数序列

    python 中的 range() 函数是很常用的,R  中相应的函数是 seq(), 其实,R 中的“ :”也能代替 python 中的 range() 函数. 1.生成升序整数序列 python: ...

  4. C#中的函数(二) 有参有返回值的函数

    接上一篇 C#中的函数(-) 无参无返回值的函数 http://www.cnblogs.com/fzxiaoyi/p/8502613.html 这次研究下C#中的函数(二) 有参有返回值的函数 依然写 ...

  5. luoguP4197:Peaks(Kruskal重构树+主席树)或者(点分树+离线)

    题意:有N座山,M条道路.山有山高,路有困难值(即点权和边权).现在Q次询问,每次给出(v,p),让求从v出发,只能结果边权<=p的边,问能够到达的山中,第K高的高度(从大到小排序). 思路:显 ...

  6. 没有该栏目数据, 可能缓存文件(/data/cache/inc_catalog_base.inc)没有更新, 请检查是否有写入权限

    今天朋友在创建新栏目时出现了一个无法生成的错误,提示没有该栏目数据, 可能缓存文件(/data/cache/inc_catalog_base.inc)没有更新, 请检查是否有写入权限,其实这个相对比较 ...

  7. Python实战之ATM+购物车

    ATM + 购物车 需求分析 ''' - 额度 15000或自定义 - 实现购物商城,买东西加入 购物车,调用信用卡接口结账 - 可以提现,手续费5% - 支持多账户登录 - 支持账户间转账 - 记录 ...

  8. Java 静态、类加载

    1.静态是什么?有什么用? static的主要作用在于创建独立于具体对象的域变量或者方法. 每创建一个对象,都会在堆里开辟内存,存成员(属性),但是不存方法,方法是共用的,没必要每一个对象都浪费内存去 ...

  9. LRU Algorithm Gym - 102394L (HASH)

    LRU Algorithm \[ Time Limit: 1000 ms\quad Memory Limit: 524288 kB \] 题意 给出 \(n\) 个数字和 \(m\) 次查询. 每次询 ...

  10. Utterance-level Aggregation for Speaker Recognition in The Wild

    文章[1]主要针对的是语句长度不定,含有不相关信号的说话人识别. 深度网络设计的关键在于主干(帧级)网络的类型[the type of trunk (frame level) network]和有时间 ...