整理: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 ...
随机推荐
- Golang 需要避免踩的 50 个坑1
最近准备写一些关于golang的技术博文,本文是之前在GitHub上看到的golang技术译文,感觉很有帮助,先给各位读者分享一下. 前言 Go 是一门简单有趣的编程语言,与其他语言一样,在使用时不免 ...
- dos2unix的使用
由于在DOS(windows系统)下,文本文件的换行符为CRLF,而在Linux下换行符为LF,使用git进行代码管理时,git会自动进行CRLF和LF之间的转换,这个我们不用操心.而有时候,我们需要 ...
- 使用jave1.0.2将amr文件转成其他格式报错解决方案
背景:最近需要将微信公众号里面用户发的语音文件转成其他格式的语音文件 介绍:在刚开始使用jave1.0.2 没有几行代码就可以实现,但是发现在转换的过程会报错,但是最后文件也转成功了,此时是在wind ...
- ArmIE的安装
参考:https://developer.arm.com/tools-and-software/server-and-hpc/arm-architecture-tools/arm-instructio ...
- IoU与非极大值抑制(NMS)的理解与实现
1. IoU(区域交并比) 计算IoU的公式如下图,可以看到IoU是一个比值,即交并比. 在分子中,我们计算预测框和ground-truth之间的重叠区域: 分母是并集区域,或者更简单地说,是预测框和 ...
- Bootstrap 提示工具(Tooltip)插件
一.提示工具(Tooltip)插件根据需求生成内容和标记,默认情况下是把提示工具(tooltip)放在它们的触发元素后面. 有以下两种方式添加提示工具(tooltip): 1.通过data属性:如需添 ...
- 第04节-BLE协议抓包演示
在上几篇博客中,形象的讲解了BLE各个层的作用,各个层的数据结构.本篇博客将研究BLE协议抓包.在实际开发中,有一个中央设备(central)和一个外设(Peripheral).所谓中央设备就是指它可 ...
- 关系型数据库 RDS(Relational Database Service),知识点
资料 网址 官方介绍 https://help.aliyun.com/document_detail/26092.html?spm=5176.2020520104.0.0.2b4b1450yqd1gg ...
- 第三章 linux常用的命令
安装笔记: 1 安装linux操作系统时,会默认创建一个超级管理员帐号:root 2 安装时,当进行到选择哪种类型的安装时,我们选择"使用所有空间"的类型 Linux概念性的东西 ...
- scrapy框架--?乱码unicode
安装 pip install scrapy 建立一个爬虫项目 scrapy startproject 项目名称 scrapy startproject itcast 进入itcast文件夹 生成一个爬 ...