整理: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 ...
随机推荐
- django logger转载
https://www.cnblogs.com/jiangchunsheng/p/8986452.html https://www.cnblogs.com/jeavy/p/10926197.html ...
- 注意力机制---Attention、local Attention、self Attention、Hierarchical attention
一.编码-解码架构 目的:解决语音识别.机器翻译.知识问答等输出输入序列长度不相等的任务. C是输入的一个表达(representation),包含了输入序列的有效信息. 它可能是一个向量,也可能是一 ...
- 借助模板类自动实现COM连接点接收器(Sink)更新
之前在借助模板类自动实现COM连接点接收器(Sink)中对原作者的代码进一步封装,弄清了连接点使用的原理,在看ATL代码的过程中,发现ATL本身就提供了AtlAdvise/AtlUnadvise这样的 ...
- css ie bug 双边距
- centos7删除PHP怎么操作
前面我们说了centos7删除MariaDB,现在我们说说centos7删除PHP怎么操作?当然不是特殊需要,不要去删除PHP,后果很严重.操作之前请做好所有的备份!首先查看有没安装php以及版本 # ...
- python 通过scapy获取网卡列表
python通过scapy 获取网卡列表如下: #coding:utf-8 from scapy.all import * #显示网卡信息 show_interfaces() 运行结果如下:
- VIJOS-P1320 清点人数
JDOJ 1427: VIJOS-P1320 清点人数 题目传送门 Description 初始时,火车上没有学生:当同学们开始上火车时,年级主任从第一节车厢出发走到最后一节车厢,每节车厢随时都有可能 ...
- ESA2GJK1DH1K基础篇: 阿里云物联网平台: 测试云平台显示MQTT客户端发过来的消息
现在这里空空如也 咱自定义的也没有数据 现在就是传上来温度数据,让这里显示温度数据 你发布的主题 /sys/a1m7er1nJbQ/Mqtt/thing/event/property/post 发布 ...
- [NOI2010]超级钢琴 主席树
[NOI2010]超级钢琴 链接 luogu 思路 和12省联考的异或粽子一样. 堆维护n个左端点,每次取出来再放回去次 代码 #include <bits/stdc++.h> #defi ...
- python总结四
sql性能优化的几种方式: 1.查询的模糊匹配: 尽量避免在一个复杂查询里面使用like '%parm%',前面的%会导致相关列的索引无法使用,最好不要用 直接修改后台,根据输入条件,先查出符合条件的 ...