1. 绑定到其它元素

<Grid>
<StackPanel>
<TextBox x:Name="textbox1" />
<Label Content="{Binding ElementName=textbox1, Path=Text}" />
</StackPanel>
</Grid>

2. 绑定到静态资源

<Window.Resources>
<ContentControl x:Key="text">Hello, World!</ContentControl>
</Window.Resources>
<Grid>
<StackPanel>
<Label x:Name="label1" Content="{Binding Source={StaticResource text}}" />
</StackPanel>
</Grid>
<STRONG>3. 绑定到自身</STRONG>
<Grid>
<StackPanel>
<Label x:Name="label1" Content="{Binding RelativeSource={RelativeSource Self}, Path=Name}" />
</StackPanel>
</Grid>

4. 绑定到指定类型的父元素

1 <Grid x:Name="Grid1">
2 <StackPanel>
3 <Label x:Name="label1" Content="{Binding RelativeSource={RelativeSource FindAncestor,
4 AncestorType={x:Type Grid}}, Path=Name}" />
5 </StackPanel>
6 </Grid>

5. 绑定到对象

1 public class Person
2 {
3 public string Name { get; set; }
4 public int Age { get; set; }
5 }
1 <StackPanel x:Name="stackPanel">
2 <StackPanel.DataContext>
3 <local:Person Name="Jack" Age="30"></local:Person>
4 </StackPanel.DataContext>
5 <TextBlock Text="{Binding Path=Name}"></TextBlock>
6 <TextBlock Text="{Binding Path=Age}"></TextBlock>
7
8 </StackPanel>
6. 绑定到集合

1 public class Person
2 {
3 public string Name { get; set; }
4 public int Age { get; set; }
5 }
6
7 public class PersonList : ObservableCollection<Person>
8 { }
01 <Window.Resources>
02 <local:PersonList x:Key="person">
03 <local:Person Name="Jack" Age="30"></local:Person>
04 <local:Person Name="Tom" Age="32"></local:Person>
05 </local:PersonList>
06 </Window.Resources>
07 <StackPanel x:Name="stackPanel">
08 <ListBox ItemsSource="{Binding Source={StaticResource ResourceKey=person}}"
09 DisplayMemberPath="Name">
10 </ListBox>
11 </StackPanel>

7. DataContext共享源

我们需要将同一资源绑定到多个 UI 元素上,很显然到处写 "{Binding Source={StaticResource person}}" 是件很繁琐且不利于修改的做法。WPF 提供了一个称之为 "数据上下文 (DataContext)" 的东西让我们可以在多个元素上共享一个源对象,只需将其放到父元素 DataContext 属性即可。当我们不给 Binding 扩展标志指定 Source 属性时,它会自动寻找上级父元素的数据上下文。

01 <Window.Resources>
02 <local:PersonList x:Key="person">
03 <local:Person Name="Jack" Age="30"></local:Person>
04 <local:Person Name="Tom" Age="32"></local:Person>
05 </local:PersonList>
06 </Window.Resources>
07 <StackPanel x:Name="stackPanel" DataContext="{StaticResource person}">
08 <ListBox ItemsSource="{Binding}"
09 DisplayMemberPath="Name">
10 </ListBox>
11 </StackPanel>

8. 使用XML作为Binding的源

XML:

01 <?xml version="1.0" encoding="utf-8" ?>
02 <PersonList>
03 <Person Id="1">
04 <Name>Jack</Name>
05 </Person>
06 <Person Id="2">
07 <Name>Tom</Name>
08 </Person>
09 <Person Id="3">
10 <Name>Justin</Name>
11 </Person>
12 <Person Id="4">
13 <Name>David</Name>
14 </Person>
15 </PersonList>
XAML:

01 <StackPanel>
02 <ListView x:Name="personListView">
03 <ListView.View>
04 <GridView>
05 <GridViewColumn Header="Id" Width="100"
06 DisplayMemberBinding="{Binding XPath=@Id}"/>
07 <GridViewColumn Header="Name" Width="100"
08 DisplayMemberBinding="{Binding XPath=Name}"/>
09 </GridView>
10 </ListView.View>
11 </ListView>
12 <Button Click="Button_Click">Load Data</Button>
13 </StackPanel>
后台代码:

01 private void Button_Click(object sender, RoutedEventArgs e)
02 {
03 XmlDocument xmlDocument = new XmlDocument();
04 xmlDocument.Load("Person.xml");
05
06 XmlDataProvider xdp = new XmlDataProvider();
07 xdp.Document = xmlDocument;
08 xdp.XPath = @"/PersonList/Person";
09
10 this.personListView.DataContext = xdp;
11 this.personListView.SetBinding(ListView.ItemsSourceProperty, new Binding());
12 }

WPF-Binding的源的更多相关文章

  1. WPF 基础 - Binding 的源与路径

    1. 源与路径 把控件作为 binding 源与 binding 标记拓展: 控制 Binding 的方向及数据更新: Binding 的路径 Path: 没有路径的 Binding: 为 Bindi ...

  2. WPF Binding值转换器ValueConverter使用简介(一)

    WPF.Silverlight及Windows Phone程序开发中往往需要将绑定的数据进行特定转换,比如DateTime类型的时间转换为yyyyMMdd的日期,再如有一个值是根据另外多组值的不同而异 ...

  3. WPF Binding

    winform有binding, WPF也有binding,区别在哪呢?这里暂时不提.以前也检查接触WPF binding, 但为什么过段时间就忘记了呢? 可能主要原因自己的知识体系不够完善吧,下面我 ...

  4. WPF Binding Mode,UpdateSourceTrigger

    WPF 绑定模式(mode) 枚举值有5个1:OneWay(源变就更新目标属性)2:TwoWay(源变就更新目标并且目标变就更新源)3:OneTime(只根据源来设置目标,以后都不会变)4:OneWa ...

  5. 深入浅出-Binding的源与路径

    1.把控件作为Binding源与Binding标记扩展<TextBox x:Name="textBox1" Text="{Binding Path=Value, E ...

  6. WPF Binding ElementName方式无效的解决方法--x:Reference绑定

    原文:WPF Binding ElementName方式无效的解决方法--x:Reference绑定 需求: 背景:Grid的有一个TextBlock name:T1和一个ListBox,ListBo ...

  7. WPF binding 参考

    Introduction This is an article on WPF Binding Cheat Sheet. Some of the Binding won't work for Silve ...

  8. Binding的源与路径

    1.把控件作为Binding的源 例子:拖动Slider,输入框中的值也会跟着改变,或在输入框中输入数值,滑动条也会自动移动 <Window x:Class="把控件作为Binding ...

  9. WPF入门教程系列(二) 深入剖析WPF Binding的使用方法

    WPF入门教程系列(二) 深入剖析WPF Binding的使用方法 同一个对象(特指System.Windows.DependencyObject的子类)的同一种属性(特指DependencyProp ...

  10. WPF Binding值转换器ValueConverter使用简介(二)-IMultiValueConverter

    注: 需要继承IMultiValueConverter接口,接口使用和IValueConverter逻辑相同. 一.MultiBinding+Converter 多值绑定及多值转换实例 当纵向流量大于 ...

随机推荐

  1. 网络流量工具iftop,ifstat

    此文非原创,转自 http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858923.html 介绍 ifstat工具是个网络接口监测工具,比较简单 ...

  2. HTMLTestRunner美化

    https://www.cnblogs.com/findyou/p/6925733.html 参考这个,美化的不错,进入了汉化,及加入了一些样式,

  3. 各种Lisp系语言大检阅

    主要特色: CommonLISP : lisp系集大成者, 工业化强度的大型语言. 拥有理论上最高的表达力, 非常复杂, 学习难度极大. 喜欢的人捧到天上, 觉得它是一切语言的终点, 不喜欢的人恶心死 ...

  4. 有关fallback diffuse unity的神奇的事情

    fallback diffuse 导致tris面片增加 double了 等于又画了一遍 很奇怪(只有一台机器是这样的) 这事情真坑我跟到最后 在一个最简测试场景 fallback diffuse替换成 ...

  5. Spark(十) -- Spark Streaming API编程

    本文测试的Spark版本是1.3.1 Spark Streaming编程模型: 第一步: 需要一个StreamingContext对象,该对象是Spark Streaming操作的入口 ,而构建一个S ...

  6. pig笔记

    1.安装Pig 将pig添加到环境变量当中 2.pig使用 首先将数据库中的数据导入到HDFS上 sqoop import --connect jdbc:mysql://192.168.1.10:33 ...

  7. spark完全分布式集群搭建

    最近学习Spark,因此想把相关内容记录下来,方便他人参考,也方便自己回忆吧 spark开发环境的介绍资料很多,大同小异,很多不能一次配置成功,我以自己的实际操作过程为准,详细记录下来. 1.基本运行 ...

  8. Windows下编译vpx获得各个项目VS2010解决方案的步骤

    最近研究了一下几种常见的视频编码器:x264,x265,vpx.本文简单记录一下它们的编译方法. x264,x265,vpx这三个开源的视频编码器可以说是当今“最火”的视频编码器.x264现在占据着H ...

  9. linux 局域网内文件传送

    Linux scp命令用于Linux之间复制文件和目录,具体如何使用这里好好介绍一下,从本地复制到远程.从远程复制到本地是两种使用方式.这里有具体举例: ================== Linu ...

  10. 深入理解C/C++ [Deep C (and C++)] (2)

    好.接着深入理解C/C++之旅.我在翻译第一篇的时候.自己是学到不不少东西,因此打算将这整个ppt翻译完成. 请看以下的代码片段: #include <stdio.h> void foo( ...