WPF绑定各种数据源之xml数据源
一、WPF绑定各种数据源索引
Binding的基础可参考WPF 绑定基础
二、WPF绑定各种数据源之xml数据源,此时的XML源写在界面了,当然也可以独立成文件。
<Window.Resources> <Con:BackgroundConverter x:Key="BackgroundConverter"/> <XmlDataProvider x:Key="myPerson3"> <x:XData> <PersonF xmlns=""> <person Name="Person1"> <ID>1</ID> <Name>XiaoA</Name> <Age>49</Age> </person> <person Name="Person2"> <ID>2</ID> <Name>XiaoB</Name> <Age>29</Age> </person> <person Name="Person3"> <ID>3</ID> <Name>XiaoC</Name> <Age>103</Age> </person> <person Name="Person4"> <ID>4</ID> <Name>XiaoD</Name> <Age>59</Age> </person> </PersonF> </x:XData> </XmlDataProvider> </Window.Resources> |
下面是绑定的代码。此时需要注意,原来用Path改成了XPath,因为这是XML源,并且ItemsSource 改成 ItemsSource="{Binding Source={StaticResource myPerson3},XPath=/PersonF/person}"
<ListView Height="262" Margin="0,32,56,0" ItemsSource="{Binding Source={StaticResource myPerson3},XPath=/PersonF/person}" VerticalAlignment="Top" Name="listView3" HorizontalAlignment="Right" Width="310"> <ListView.View> <GridView> <GridViewColumn Header="编号" DisplayMemberBinding="{Binding XPath=ID}" Width="100" /> <GridViewColumn Header="姓名" DisplayMemberBinding="{Binding XPath=Name}" Width="100"/> <GridViewColumn Header="年龄" Width="100"> <GridViewColumn.CellTemplate> <DataTemplate> <TextBlock Grid.Column="1" Text="{Binding XPath=Age}" Foreground="{Binding XPath=Age, Converter={StaticResource BackgroundConverter}}"/> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> </GridView> </ListView.View> </ListView> |
下面是值转换
public class BackgroundConverter : IValueConverter { #region IValueConverter Members public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { Color color = new Color(); int num = int.Parse(value.ToString()); if (num > 100) color = Colors.Yellow; else if (num < 50) color = Colors.LightGreen; else color = Colors.LightPink; return new SolidColorBrush(color); } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } #endregion } |
效果图:
2.使用外部XML数据源,只需做如下修改
<Window.Resources> <XmlDataProvider x:Key="myPerson3" Source="/Person.xml"/> </Window.Resources> |
3.如果使用外部XML数据源,并且采用C#代码的形式,则如下:
XmlDocument doc = new XmlDocument(); doc.Load(@"http://www.cnblogs.com/XMLFile1.xml"); XmlDataProvider provider = new XmlDataProvider(); provider.Document = doc; provider.XPath = @"/PersonF/person"; listView3.DataContext = provider; listView3.SetBinding(ListView.ItemsSourceProperty, new Binding()); |
当然也可以使用XMLDataProvider的Source属性,此时只需做如下修改:
XmlDataProvider provider = new XmlDataProvider();provider.Source = new Uri(@"F:\\XMLFile1.xml");provider.XPath = @"/PersonF/person"; |
其他不变。
作者:Work Hard Work Smart
出处:http://www.cnblogs.com/linlf03/
欢迎任何形式的转载,未经作者同意,请保留此段声明!
WPF绑定各种数据源之xml数据源的更多相关文章
- WPF绑定各种数据源之object数据源
一.WPF绑定各种数据源索引 WPF 绑定各种数据源之Datatable WPF绑定各种数据源之object数据源 WPF绑定各种数据源之xml数据源 WPF绑定各种数据源之元素控件属性 Bindin ...
- WPF 绑定以基础数据类型为集合的无字段名的数据源
WPF 绑定以基础数据类型为集合的无字段名的数据源 运行环境:Window7 64bit,.NetFramework4.61,C# 6.0: 编者:乌龙哈里 2017-02-21 我们在控件的数据绑定 ...
- XML数据源快速开发框架——XmlFramwork
浪漫的周末从cnblogs开始.话说,今天和往常的周末一样,韩君躲在被窝里用手机翻阅着园子里的珠玑.一篇<应用XML作为数据库的快速开发框架>的文章在韩君脑子里激起了一波球形闪电.想想上周 ...
- coreseek(sphinx)安装1(xml数据源配置和测试)
1.下载coreseek-3.2.14-32版本.网址:http://www.coreseek.cn/products-install/install_on_windows/ (有详细的安装说明) ...
- WPF快速入门系列(4)——深入解析WPF绑定
一.引言 WPF绑定使得原本需要多行代码实现的功能,现在只需要简单的XAML代码就可以完成之前多行后台代码实现的功能.WPF绑定可以理解为一种关系,该关系告诉WPF从一个源对象提取一些信息,并将这些信 ...
- 【转】【WPF】WPF绑定用法
一.简介 为了后面行文顺利,在进入正文之前,我们首先对本文所涉及到的绑定知识进行简单地介绍.该节包含绑定的基本组成以及构建方式. WPF中的绑定完成了绑定源和绑定目标的联动.一个绑定常常由四部分组成: ...
- WPF - 绑定及惯用法(一)
写在前面:这仍然是一些没有经过严格审阅的文字.虽然我的确执行了初稿.复稿以及审阅等一系列用以保证文章质量的方法,但是仍然担心其中是否有错误.希望您能帮助指出,以在下一次我在版本更新时进行修正.所有的错 ...
- WPF绑定的ListBox获取ListBoxItem及GoToState应用
现公司项目中需要制作一个扇形菜单,菜单项是用ListBox重写Style实现的,其数据是绑定的.菜单的每一项都有Normal,MouseOver和Selected三种状态,这三种状态当然可以通过鼠标移 ...
- 43. Spring Boot动态数据源(多数据源自动切换)【从零开始学Spring Boot】
[视频&交流平台] àSpringBoot视频 http://study.163.com/course/introduction.htm?courseId=1004329008&utm ...
随机推荐
- 13 Spring Boot Shiro使用JS-CSS-IMG
filterChainMap.put("/403", "anon");filterChainMap.put("/assets/**", &q ...
- Vue在移动端App中使用的问题总结
1.客户端中弹出键盘使得fixed布局错乱 Vue 在移动端中使用,当弹出键盘时,fixed 布局的元素可能会被键盘顶起. 例子图示及解决方法参考:https://blog.csdn.net/qq_3 ...
- 【PowerOJ1737&网络流24题】太空飞行计划问题(最小割)
题意: 思路: #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned in ...
- 封装通用的 ajax, 基于 jQuery。
在前端异步获取数据时候每次都是使用 ajax:为了通用性更好,然而封装通用的 ajax 是一个一劳永逸的办法. 本次基于 jQuery 封装实现: 第一步: 引入 jQuery: <script ...
- twitter api的使用
1.用手机号注册推特账号 https://twitter.com/ 2.进入网站 https://apps.twitter.com/ 创建第一个app,填入基本信息 name写完会检测是否已经存在像我 ...
- 深入探究JVM(1) - Java的内存区域解析
http://blog.csdn.net/sczyh22/article/details/46652901<br>Java 虚拟机在执行Java程序的时候会把它管理的内存区域划为几部分,这 ...
- windows下数据库备份bat
@echo offset "Ymd=%date:~,4%%date:~5,2%%date:~8,2%"C:/mysql/bin/mysqldump --opt -u root -- ...
- debugfs linux rm 删除 恢复 Attempt to read block from filesystem resulted in short read while opening filesystem
w 删除具有空字符的文件 反斜杠来转义下一个字符 rm -R Samples\ -\ Copy well@well:/home/etc/project/apilinux/MarketplaceWebS ...
- delphi 访问 protected 属性 哈哈
unit Unit39; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, Syste ...
- pyAudioAnalysis-audioFeatureExtraction 错误纠正
1. TypeError: mfccInitFilterBanks() takes 2 positional arguments but 7 were given The issue In the f ...