整理:WPF中XmlDataProvider的用法总结
原文:整理:WPF中XmlDataProvider的用法总结
一、目的:了解XmlDataProvider中绑定数据的方法
二、绑定方式主要有三种:
1、Xaml资源中内置:
-
<!--XPath指定一组节点-->
-
<XmlDataProvider x:Key="ds1" XPath="XUnits">
-
<x:XData>
-
<XUnits xmlns="">
-
<FFmpegCommandTextParameter Text="设置纪录时间" ToolTip="hh:mm:ss[.xxx]格式的记录时间也支持" Command="-t" Parameter="2001"/>
-
<FFmpegCommandTextParameter Text="搜索到指定的时间" ToolTip="[-]hh:mm:ss[.xxx]的格式也支持" Command="-ss" Parameter="2001"/>
-
<FFmpegCommandTextParameter Text="设置标题" ToolTip="设置标题" Command="-title" Parameter="2001"/>
-
<FFmpegCommandTextParameter Text="设置作者" ToolTip="设置作者" Command="-author" Parameter="2001"/>
-
<FFmpegCommandTextParameter Text="设置版权" ToolTip="设置版权" Command="-copyright" Parameter="2001"/>
-
<FFmpegCommandTextParameter Text="设置评论" ToolTip="设置评论" Command="-comment" Parameter="2001"/>
-
<FFmpegCommandCheckParameter Text="激活高质量设置" ToolTip="激活高质量设置" Command="-hq" IsChecked="false"/>
-
-
<FFmpegCommandTextParameter Text="设置目标文件类型" ToolTip="设置目标文件类型" Command="-author" Parameter="2001"/>
-
<FFmpegCommandCheckParameter Text="激活高质量设置" ToolTip="激活高质量设置" Command="-hq" IsChecked="false"/>
-
</XUnits>
-
</x:XData>
-
</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、绑定树形结构
-
<!--年级模版-->
-
<HierarchicalDataTemplate DataType="Grade" ItemsSource="{Binding XPath=Class}">
-
<TextBlock Text="{Binding XPath=@Name}"></TextBlock>
-
</HierarchicalDataTemplate>
-
<!--班级模版-->
-
<HierarchicalDataTemplate DataType="Class" ItemsSource="{Binding XPath=Group}">
-
<RadioButton Content="{Binding XPath=@Name}"></RadioButton>
-
</HierarchicalDataTemplate>
-
<!--分组模版-->
-
<HierarchicalDataTemplate DataType="Group">
-
<CheckBox Content="{Binding XPath=@Name}"></CheckBox>
-
</HierarchicalDataTemplate>
-
<!--数据模版-->
-
<XmlDataProvider x:Key="ds" XPath="Data/Grade">
-
<x:XData>
-
<Data xmlns="">
-
<Grade Name="一年级">
-
<Class Name="甲班">
-
<Group Name="A组"></Group>
-
<Group Name="B组"></Group>
-
<Group Name="C组"></Group>
-
</Class>
-
<Class Name="乙班">
-
<Group Name="A组"></Group>
-
<Group Name="B组"></Group>
-
<Group Name="C组"></Group>
-
</Class>
-
</Grade>
-
<Grade Name="二年级">
-
<Class Name="丙班">
-
<Group Name="A组"></Group>
-
<Group Name="B组"></Group>
-
<Group Name="C组"></Group>
-
</Class>
-
<Class Name="丁班">
-
<Group Name="A组"></Group>
-
<Group Name="B组"></Group>
-
<Group Name="C组"></Group>
-
</Class>
-
</Grade>
-
</Data>
-
</x:XData>
-
</XmlDataProvider>
-
-
-
-
<Menu ItemsSource="{Binding Source={StaticResource ds}}"></Menu>
-
<TreeView ItemsSource="{Binding Source={StaticResource ds}}" Margin="5"></TreeView>
2、绑定到ListBox、Comboboxl并设置模板
-
<!--Xml中的元素名可以作为DataType-->
-
<DataTemplate DataType="XUnit">
-
<Grid>
-
<StackPanel Orientation="Horizontal">
-
<Grid>
-
<Rectangle Fill="Red" Width="{Binding XPath=@Price}" Stroke="Yellow"></Rectangle>
-
<TextBlock Text="{Binding XPath=@Year}"/>
-
</Grid>
-
<TextBlock Text="{Binding XPath=@Price}"></TextBlock>
-
</StackPanel>
-
</Grid>
-
</DataTemplate>
-
<!--XPath指定一组节点-->
-
<XmlDataProvider x:Key="ds1" XPath="XUnits/XUnit">
-
<x:XData>
-
<XUnits xmlns="">
-
<XUnit Price="100" Year="2001"></XUnit>
-
<XUnit Price="120" Year="2002"></XUnit>
-
<XUnit Price="140" Year="2003"></XUnit>
-
<XUnit Price="180" Year="2004"></XUnit>
-
<XUnit Price="150" Year="2005"></XUnit>
-
<XUnit Price="200" Year="2006"></XUnit>
-
</XUnits>
-
</x:XData>
-
</XmlDataProvider>
四、设置XPath
1、直接设置类型的XPath对应的属性
-
<DataTemplate DataType="FFmpegCommandTextParameter">
-
<Grid Width="{Binding RelativeSource={RelativeSource AncestorType=ListBox},Path=ActualWidth}">
-
<Grid>
-
<Grid.ColumnDefinitions>
-
<ColumnDefinition/>
-
<ColumnDefinition Width="2*"/>
-
</Grid.ColumnDefinitions>
-
-
<TextBlock Text="{Binding XPath=@Text}" ToolTip="{Binding XPath=@ToolTip}"/>
-
<TextBox Text="{Binding XPath=@Parameter}" ToolTip="{Binding XPath=@Command}" Grid.Column="1"/>
-
</Grid>
-
</Grid>
-
</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>2]}" />
-
<ListBox.ItemsSource>
-
<Binding Source="{StaticResource InventoryData}"
-
XPath="*[@Stock='out'] | *[@Number>=8 or @Number=3]"/>
-
</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、匹配所有类型
-
<ListBox ScrollViewer.HorizontalScrollBarVisibility="Hidden">
-
<ListBox.ItemsSource>
-
<Binding Source="{StaticResource ds1}" XPath="*"/>
-
</ListBox.ItemsSource>
-
</ListBox>
五、数据同步到XML
-
XmlDataProvider xml = this.Resources["xmlData"] as XmlDataProvider;
-
xml.Document.Save("D:/temp.xml");
六、绑定到TextBlock
数据源一:
-
<XmlDataProvider x:Key="ss" >
-
<x:XData>
-
<curriculum_info xmlns="" class_name = "高等数学" teacher = "tttt" place ="24楼301室" time="31">34353</curriculum_info>
-
</x:XData>
-
</XmlDataProvider>
绑定到属性:
<TextBlock Text="{Binding Source={StaticResource ss}, XPath=curriculum_info/@class_name}"/>
绑定到节点:
<TextBlock Text="{Binding Source={StaticResource ss}, XPath=curriculum_info}"/>
数据源二:
-
-
<XmlDataProvider x:Key="ss" XPath="curriculum_info">
-
<x:XData>
-
<curriculum_info xmlns="" class_name = "高等数学" teacher = "吕良福" place ="24楼301室" time="31">34353</curriculum_info>
-
</x:XData>
-
</XmlDataProvider>
绑定到属性:
<TextBlock Text="{Binding Source={StaticResource ss}, XPath=@class_name}"/>
数据源三:
-
<XmlDataProvider x:Key="ss" XPath="List">
-
<x:XData>
-
<List xmlns="">
-
<curriculum_info xmlns="" class_name = "高等数学" teacher = "ttt" place ="24楼301室" time="31">34353</curriculum_info>
-
</List>
-
</x:XData>
-
</XmlDataProvider>
<TextBlock Text="{Binding Source={StaticResource ss},XPath=curriculum_info}"/>
注意:xmlns=""必须含有(重要)
整理:WPF中XmlDataProvider的用法总结的更多相关文章
- 整理:WPF中CommandBindings的用法
原文:整理:WPF中CommandBindings的用法 目的:了解一下CommandBindings.InputBindings.ICommandSource中在WPF中扮演什么样的角色 Comma ...
- WPF中StringFormat的用法
原文:WPF中StringFormat的用法 WPF中StringFormat的用法可以参照C#中string.Format的用法 1. C#中用法: 格式化货币(跟系统的环境有关,中文系统默认格式化 ...
- WPF中StringFormat的用法--显示特定位数的数字
原文:WPF中StringFormat的用法--显示特定位数的数字 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/huangli321456/art ...
- WPF中log4net的用法
WPF中如何使用log4nethttp://www.cnblogs.com/C-Sharp2/archive/2013/04/12/WPF-LOG4NET.html Apache log4net Ma ...
- wpf中INotifyPropertyChanged的用法
using System;using System.Collections.Generic;using System.ComponentModel;using System.Linq;using Sy ...
- 整理:WPF中Binding的几种写法
原文:整理:WPF中Binding的几种写法 目的:整理WPF中Bind的写法 <!--绑定到DataContext--> <Button Content="{Bindin ...
- 在 WPF 中的线程
线程处理使程序能够执行并发处理,以便它可以做多个操作一次.节省开发人员从线程处理困难的方式,设计了 WPF (窗口演示文稿基金会).这篇文章可以帮助理解线程在 WPF 中的正确用法. WPF 内部线程 ...
- WPF中的常用布局 栈的实现 一个关于素数的神奇性质 C# defualt关键字默认值用法 接口通俗理解 C# Json序列化和反序列化 ASP.NET CORE系列【五】webapi整理以及RESTful风格化
WPF中的常用布局 一 写在开头1.1 写在开头微软是一家伟大的公司.评价一门技术的好坏得看具体的需求,没有哪门技术是面面俱到地好,应该抛弃对微软和微软的技术的偏见. 1.2 本文内容本文主要内容 ...
- 转:WPF中ListBox的创建和多种绑定用法
先从最容易的开始演示ListBox控件的创建. Adding ListBox Items下面的代码是向ListBox控件中添加多项ListBoxItem集合.XAML代码如下:<ListBox ...
随机推荐
- rz上传文件乱码
问题 使用rz上传文件时,出现乱码,上传失败. 原因 文件中包含控制字符 解决方法 使用 rz -be -b:–binary 用binary的方式上传下载,不解释字符为ascii: -e:–esca ...
- 【Servlet】Servlet的配置
创建时间:6.15 Servlet的配置 1. 基本配置 其中url-pattern的配置方式: 1)完全匹配 访问的资源与配置的资源完全相同才能访问到 2)目录匹配 格式:/虚拟的目录../* ...
- java 深copy
public static<T> T deepClone(T src) throws IOException, ClassNotFoundException { Object obj = ...
- KL散度=交叉熵-熵
熵:可以表示一个事件A的自信息量,也就是A包含多少信息. KL散度:可以用来表示从事件A的角度来看,事件B有多大不同. 交叉熵:可以用来表示从事件A的角度来看,如何描述事件B. 一种信息论的解释是: ...
- java中的this关键字三种作用
1.表示类中的属性和调用方法2.调用本类中的构造方法3.表示当前对象
- nginx 其他配置语法
1.nginx 缓冲区配置 2.跳转重定向 3.头信息 4.超时 location / { proxy_pass http://127.0.0.1:8080;(代理跳转url) proxy_redir ...
- 【转】编写高质量代码改善C#程序的157个建议——建议56:使用继承ISerializable接口更灵活地控制序列化过程
建议56:使用继承ISerializable接口更灵活地控制序列化过程 接口ISerializable的意义在于,如果特性Serializable,以及与其像配套的OnDeserializedAttr ...
- IComparable和IComparer接口
C#中,自定义类型,支持比较和排序,需要实现IComparable接口.IComparable接口存在一个名为CompareTo()的方法,接收类型为object的参数表示被比较对象,返回整型值:1表 ...
- RabbitMQ消息确认机制
文章目录 1. 事务机制2. Confirm模式2.1 生产者2.1.1 普通Confirm模式2.1.2 批量Confirm模式2.1.3 异步Confirm模式2.2 消费者3. 其他 消费者如何 ...
- JSPDF支持中文(思源黑体)采坑之旅,JSPDF中文字体乱码解决方案
我拍个砖,通常标称自己文章完美解决何种问题的,往往就是解决不了任何问题! 众所周知,JSPDF是一个开源的,易用的,但是对中文支持非常差的PDF库. 下面,我教大家,如何在pdf中使用思源黑体.思源黑 ...