整理: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 ...
随机推荐
- 尚硅谷MySQL高级学习笔记
目录 数据库MySQL学习笔记高级篇 写在前面 1. mysql的架构介绍 mysql简介 mysqlLinux版的安装 mysql配置文件 mysql逻辑架构介绍 mysql存储引擎 2. 索引优化 ...
- cookie跨域解决方案
cookie的名/值对中的值不允许出现分号.逗号和空白符,因此在设置cookie前要用encodeURIComponent()编码,读取时再用decodeURIComponent()解码. cooki ...
- 是否忘记了向源中添加“#include "StdAfx.h"”?
错误分析 此错误发生的原因是编译器在寻找预编译指示头文件(默认#include "stdafx.h")时,文件未预期结束.没有找到预编译指示信息的头文件"stdafx.h ...
- nginx 缓存服务
1.nginx 缓存 upstream imooc { server 116.62.103.228:8001; server 116.62.103.228:8002; server 116.62.10 ...
- mybatis框架-resultMap的自动映射级别-partial 和full的探讨
现在我们做一个小实验,输出一下上一个案例中没有匹配的属性,注意哦,现在user类中是有内部嵌套的复杂数据类型的 运行结果: 注意到:现在居然连userPassword都打印不出来了,原因就是user类 ...
- django @login_required登录限制(2)-返回登陆成功后的页面
本次要实现的功能是,访问未登录的视图函数,需要先跳转到登录页面,登陆成功在跳转回来. 之前在网上找了很多资料,都没有找到解决方案. 跳转到登录页面很好弄,就是登陆成功跳转回来出了问题,原因是登录后的p ...
- django -- ORM查询
前戏 在我们之前操作ORM中,你也许是启动Django项目,通过地址访问固定的函数,或者在pycharm里的python console里执行,第一种比较麻烦,而且每次都要启动项目,写路由,第二种虽然 ...
- 洛谷P2704 [NOI2001]炮兵阵地题解
题目描述 司令部的将军们打算在\(N * M\)的网格地图上部署他们的炮兵部队.一个\(N * M\)的地图由N行M列组成,地图的每一格可能是山地(用\("H"\) 表示),也可能 ...
- 【LG3647】[APIO2014]连珠线
[LG3647][APIO2014]连珠线 题面 洛谷 题解 首先考虑一下蓝线连起来的情况,一定是儿子-父亲-另一个儿子或者是儿子-父亲-父亲的父亲. 而因为一开始只有一个点在当前局面上,将一条红边变 ...
- gethostbyaddr
函数原型: #include<netdb.h> struct hostent * gethostbyaddr(const char *addr, socklen_t len, int fa ...