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. ZOJ 3792 Romantic Value 最小割(最小费用下最小边数)

    求最小割及最小花费 把边权c = c*10000+1 然后跑一个最小割,则flow / 10000就是费用 flow%10000就是边数. 且是边数最少的情况.. #include<stdio. ...

  2. Unity GPU Query OpenGLES 3.0

    https://github.com/google/render-timing-for-unity/blob/master/RenderTimingPlugin/RenderTimingPlugin. ...

  3. Ubuntu Tomcat

    启动80端口: 修改server.xml  Connector port=80 修改/etc/default/tomcat  AUTHBIND=yes 查看/etc/authbind/byport/下 ...

  4. PhantomJS用法示例

    收录待用,修改转载已取得腾讯云授权 前言 大家有没有发现之前我们写的爬虫都有一个共性,就是只能爬取单纯的html代码,如果页面是JS渲染的该怎么办呢?如果我们单纯去分析一个个后台的请求,手动去摸索JS ...

  5. netty4 ServerBootstrap.bind(port) debug

    代码是netty4自带的例子 我们在bind的行前加个断电,下面是ServerBootstrap.bind(port)方法所经历的一些步骤. 在AbstractBootstrap.initAndReg ...

  6. docker实战——构建Jekyll

    构建第一个应用 要构建的第一个应用是Jekyll框架的自定义网站.我们会构建一下两个镜像. 一个镜像安装Jekyll以及其他用于构建Jekyll网站的必要的软件包. 一个镜像通过Apache来让Jek ...

  7. GDB基本命令(整合)(转)

    directory:添加源文件目录 l src.cpp:line_num可进入文件 如:l src.cpp:10 回车自动重复上一命令 一.gdb调试基本知识a.调试器指示的是将要执行的代码行b.只有 ...

  8. 开源静态分析工具androguard体验

    原文链接:http://blog.csdn.net/xbalien29/article/details/21885297 虽然在windows端免费版的IDA.VTS等工具都可用来静态分析,但相对来说 ...

  9. C#实现的根据日期得到今天是星期几

    算法如下:   基姆拉尔森计算公式: W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7  在公式中d表示日期中的日数,m表示月份数,y表示年数.注意:在公式中有 ...

  10. STM32 通用T2、T3、T4、T5定时器详解

    定时器初始化配置 void TIM3_Configuration(void)//1MS { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_DeI ...