一、WPF绑定各种数据源索引

WPF 绑定各种数据源之Datatable

WPF绑定各种数据源之object数据源

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数据源的更多相关文章

  1. WPF绑定各种数据源之object数据源

    一.WPF绑定各种数据源索引 WPF 绑定各种数据源之Datatable WPF绑定各种数据源之object数据源 WPF绑定各种数据源之xml数据源 WPF绑定各种数据源之元素控件属性 Bindin ...

  2. WPF 绑定以基础数据类型为集合的无字段名的数据源

    WPF 绑定以基础数据类型为集合的无字段名的数据源 运行环境:Window7 64bit,.NetFramework4.61,C# 6.0: 编者:乌龙哈里 2017-02-21 我们在控件的数据绑定 ...

  3. XML数据源快速开发框架——XmlFramwork

    浪漫的周末从cnblogs开始.话说,今天和往常的周末一样,韩君躲在被窝里用手机翻阅着园子里的珠玑.一篇<应用XML作为数据库的快速开发框架>的文章在韩君脑子里激起了一波球形闪电.想想上周 ...

  4. coreseek(sphinx)安装1(xml数据源配置和测试)

    1.下载coreseek-3.2.14-32版本.网址:http://www.coreseek.cn/products-install/install_on_windows/   (有详细的安装说明) ...

  5. WPF快速入门系列(4)——深入解析WPF绑定

    一.引言 WPF绑定使得原本需要多行代码实现的功能,现在只需要简单的XAML代码就可以完成之前多行后台代码实现的功能.WPF绑定可以理解为一种关系,该关系告诉WPF从一个源对象提取一些信息,并将这些信 ...

  6. 【转】【WPF】WPF绑定用法

    一.简介 为了后面行文顺利,在进入正文之前,我们首先对本文所涉及到的绑定知识进行简单地介绍.该节包含绑定的基本组成以及构建方式. WPF中的绑定完成了绑定源和绑定目标的联动.一个绑定常常由四部分组成: ...

  7. WPF - 绑定及惯用法(一)

    写在前面:这仍然是一些没有经过严格审阅的文字.虽然我的确执行了初稿.复稿以及审阅等一系列用以保证文章质量的方法,但是仍然担心其中是否有错误.希望您能帮助指出,以在下一次我在版本更新时进行修正.所有的错 ...

  8. WPF绑定的ListBox获取ListBoxItem及GoToState应用

    现公司项目中需要制作一个扇形菜单,菜单项是用ListBox重写Style实现的,其数据是绑定的.菜单的每一项都有Normal,MouseOver和Selected三种状态,这三种状态当然可以通过鼠标移 ...

  9. 43. Spring Boot动态数据源(多数据源自动切换)【从零开始学Spring Boot】

    [视频&交流平台] àSpringBoot视频 http://study.163.com/course/introduction.htm?courseId=1004329008&utm ...

随机推荐

  1. 数组Array方法: indexOf、filter、forEach、map、reduce使用实例

  2. mybatis动态update语句

  3. mysql SELECT语句 语法

    mysql SELECT语句 语法,苏州大理石方箱 作用:用于从表中选取数据.结果被存储在一个结果表中(称为结果集). 语法:SELECT 列名称 FROM 表名称 以及 SELECT * FROM ...

  4. 在Python中,如何将一个字符串数组转换成整型数组

    https://blog.csdn.net/xiangchengguan/article/details/78987041 arr = ['] arr = list(map(int,arr)) pri ...

  5. Miniprofiler在目中使用报 mini-profiler-resources/includes.js 404错误

    原因,没有配置webconfig <system.webServer> <modules> <remove name="FormsAuthentication& ...

  6. 创建maven web项目时,没有web.xml文件

    1.问题:创建maven项目时,选择的是创建web-app项目,但是结果配置之后,却没有web.xml文件. 2.解决办法: ------------------------------------- ...

  7. Java基础之数组类型

    对于Java,前面的一些基础概念不是很想写,看了看还是从数组开始写吧(毕竟数组是第一个引用类型,相对复杂一点),我也是学了JAVA不是很久,目前看完了JAVA的基础视频,还有JAVA疯狂讲义这本书的大 ...

  8. [BZOJ4552]:[Tjoi2016&Heoi2016]排序(桶排序)

    题目传送门 题目描述 在2016年,佳媛姐姐喜欢上了数字序列. 因而她经常研究关于序列的一些奇奇怪怪的问题,现在她在研究一个难题,需要你来帮助她. 这个难题是这样子的:给出一个1到n的全排列,现在对这 ...

  9. 转载自:StringUtils的常见方法

    转载自:https://blog.csdn.net/simple_smile_sun/article/details/51819158 注:运用StringUtils需要导入相关jar文件,commo ...

  10. php部署调优

    转自Laravel学院,  作者:学院君 最近刚好看到一些php.ini优化问题处理. 很多文章都是把配置全部翻译. (内容翻译太多和流程结构写的不是很清晰,看起来也头大.但是建议全部内容看几遍了解一 ...