在WPF应用的开发过程中Binding是一个非常重要的部分。

在实际开发过程中Binding的不同种写法达到的效果相同但事实是存在很大区别的。

这里将实际中碰到过的问题做下汇总记录和理解。

1. source = {binding} 和source = {binding RelativeSource={RelativeSource self},Path=DataContext}效果相同

理解:{binding} 不设定明确的绑定的source,这样binding就去从本控件类为开始根据可视树的层次结构自下而上查找不为空的Datacontext属性的值。

{binding RelativeSource={RelativeSource self},Path=DataContext}中RelativeSource self的含义为绑定的source为控件自身,这样binding 就绑定了自身控件的Datacontext。

效果:

  1. <StackPanel DataContext="abc">
  2. <Label Content="{Binding}"></Label>
  3. <Label Content="{Binding RelativeSource={RelativeSource Self},Path=DataContext}"></Label>
  4. </StackPanel>

  1. <StackPanel DataContext="abc">
  2. <Label Content="{Binding}"></Label>
  3. <Label DataContext="def" Content="{Binding RelativeSource={RelativeSource Self},Path=DataContext}"></Label>
  4. </StackPanel>

2.在Template的Trigger中改变Template中某个样式控件的属性

  1. <Style TargetType="{x:Type Button}">
  2. <Setter Property="Template">
  3. <Setter.Value>
  4. <ControlTemplate TargetType="{x:Type Button}">
  5. <Border>
  6. <Label x:Name="PART_Label" Content="{TemplateBinding ContentA}" />
  7. </Border>
  8. <ControlTemplate.Triggers>
  9. <Trigger Property="IsChecked" Value="True">
  10. 注: <Setter TargetName="PART_Label" Property="Content" Value="{Binding Path=ContentB, RelativeSource={RelativeSource TemplatedParent}}" />
  11. </Trigger>
  12. </ControlTemplate.Triggers>
  13. </ControlTemplate>
  14. </Setter.Value>
  15. </Setter>
  16. </Style>

当然把注:的这句改成<Setter TargetName="PART_Label" Property="Content" Value="{Binding Path=ContentB, RelativeSource={RelativeSource AncestorType={x:Type Button}}}">效果一样。

当一个Binding有明确的数据来源时可以通过为Source或ElementName赋值的办法让Binding与之关联,有的时候由于不能确定Source的对象叫什么名字,但知道它与作为Binding目标的对象在UI布局上有相对关系,比如控件自己关联自己的某个数据、关联自己某级容器的数据,就要使用Binding的RelativeSource属性。RelativeSource属性的数据类型为RelativeSource类,通过这个类的几个静态或非静态属性可以控制它搜索相对数据源的方式。

我们来看一看Elementname,Source,RelativeSource 三种绑定的方式

1.ElementName顾名思义就是根据Ui元素的Name来进行绑定:

例子:

  1. <Window x:Name="MainWindow">
  2. <Grid>
  3. <Button Background=”{Binding ElementName=MainWindow, Path=Background}”/>
  4. </Grid>
  5. </Window>
  6. 效果等同于
  7. <Window>
  8. <Grid>
  9. <Button Background=”{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window},Path=Background}”/>
  10. </Grid>
  11. </Window>

区别:

ElementName属性用于引用一个UI对象的名称,其的作用域在同一XAML文件内,不能引用另一XAML文件的某个Ui元素名。

2.Source属性用于指定对象绑定路径的引用。 其特点是:Source属性通常用于绑定设置的对象时,是已知的。

  1. <Window x:Name="MainWindow">
  2. <Grid>
  3. <Button Background=”{Binding Source={StaticResource ButtonStyle}}”/>
  4. </Grid>
  5. </Window>

3.RelativeSource

在不确定绑定的Source时,但知道与绑定对象两者相对关系时就需要使用RelativeSource,这也是RelativeSource 与ElementName和Source的最大区别。

RelativeSource 的三种典型用法:

/1.UI元素的一个属性绑定在自身的另一个属性上

  1. <Label Background = {Binding Path=Forgroud, RelativeSource={RelativeSource Self}} />

/2.UI元素的一个属性绑定在某个父元素的属性上

  1. <Grid>
  2. <Label Background = {Binding Path=Background, RelativeSource={RelativeSource AncestorType={x:Type Grid}}}/>
  3. </Grid>

/3.Template中的元素的属性绑定在Template使用者元素的属性上

  1. {Binding Path=PathToProperty, RelativeSource={RelativeSource TemplatedParent}}

例子:

  1. <Style TargetType="{x:Type local:NumericUpDown}">
  2. <Setter Property="HorizontalAlignment" Value="Center"/>
  3. <Setter Property="VerticalAlignment" Value="Center"/>
  4. <Setter Property="Template">
  5. <Setter.Value>
  6. <ControlTemplate TargetType="{x:Type local:NumericUpDown}">
  7. <Grid Margin="">
  8. <Grid.RowDefinitions>
  9. <RowDefinition/>
  10. <RowDefinition/>
  11. </Grid.RowDefinitions>
  12. <Grid.ColumnDefinitions>
  13. <ColumnDefinition/>
  14. <ColumnDefinition/>
  15. </Grid.ColumnDefinitions>
  16. <Border BorderThickness="" BorderBrush="Gray"
  17. Margin="" Grid.RowSpan=""
  18. VerticalAlignment="Center" HorizontalAlignment="Stretch">
  19.  
  20. <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value}"
  21. Width="" TextAlignment="Right" Padding=""/>
  22. </Border>
  23. </Grid>
  24. </ControlTemplate>
  25. </Setter.Value>
  26. </Setter>
  27. </Style>

利用TemplateBinding 绑定模板与原对象之间的属性

{TemplateBinding Path=PathToProperty}

例子:

  1. <ControlTemplate TargetType="{x:Type Button}" x:Key="buttonTemp">
  2. <Border BorderThickness="" Background="{TemplateBinding Foreground}">
  3. <TextBlock Foreground="{TemplateBinding Background}"/>
  4. </Border>
  5. </ControlTemplate>

转载时,请注明本文来源:www.cnblogs.com/tmywu

Binding 中 Elementname,Source,RelativeSource 三种绑定的方式的更多相关文章

  1. javascript中var let const三种变量声明方式

    javascript中var let const三种变量声明方式 1.var  ①var表示声明了一个变量,并且可以同时初始化该变量. ②使用var语句声明的变量的作用域是当前执行位置的上下文:一个函 ...

  2. spring boot:thymeleaf模板中insert/include/replace三种引用fragment方式的区别(spring boot 2.3.3)

    一,thymeleaf模板中insert/include/replace三种引用fragment方式的区别 insert: 把整个fragment(包括fragment的节点tag)插入到当前节点内部 ...

  3. 后台将数据传回前台的三种绑定的方式(Model,Map.ModelAndView)

    //方式1:通过model 将数据绑定 @RequestMapping(value = "findByIdModel", method = RequestMethod.GET) p ...

  4. js this详解,事件的三种绑定方式

    this,当前触发事件的标签 在绑定事件中的三种用法: a. 直接HTML中的标签里绑定 onclick="fun1()"; b. 先获取Dom对象,然后利用dom对象在js里绑定 ...

  5. Dom事件的三种绑定方式

    1.事件 2.  onclick, onblur, onfocus, 需求:请写出一个行为,样式,结构,相分离的页面.   JS,   CSS,  HTML, 示例1,行为结构样式粘到一起的页面: & ...

  6. jQuery中detach&&remove&&empty三种方法的区别

    jQuery中empty&&remove&&detach三种方法的区别 empty():移除指定元素内部的所有内容,但不包括它本身 remove():移除指定元素内部的 ...

  7. Hibernate中Java对象的三种状态

                                                                                     Hibernate中Java对象的三种 ...

  8. .NET中的三种接口实现方式

    摘自:http://www.cnblogs.com/zhangronghua/archive/2009/11/25/1610713.html 一般来说.NET提供了三种不同的接口实现方式,分别为隐式接 ...

  9. C语言中最常用的三种输入输出函数scanf()、printf()、getchar()和putchar()

    本文给大家介绍C语言中最常用的三种输入输出函数scanf().printf().getchar()和putchar(). 一.scanf()函数格式化输入函数scanf()的功能是从键盘上输入数据,该 ...

随机推荐

  1. SQLite 对中文路径的支持(用到了StringToWideChar和Utf8Encode在D7的System单元中自带)

    最近用SQLITE作为数据库,发现,如果直接传递带中文路径或文件名的数据库,会导致无法打开数据库的情况.看了一下SQLITE的源码,才发现,原来SQLITE中是用UTF8编码进行文件打开操作的. 所以 ...

  2. block的是发送信号的线程,又不是处理槽函数的线程

    请问UI线程给子线程发信号,应该用哪种连接方式? 如果子线程正在执行一个函数,我发射信号去执行子线程的另一个函数,那么此时子线程到底会执行什么呢? 用信号量做的同步.第一把信号槽的事件丢到线程的事件队 ...

  3. Bootstrap框架中的字形图标的理解

    最近项目中准备使用 Bootstrap 框架,看中了Ace Admin 这套皮肤,看其代码的时候,发现使用了字形图标.下面内容来源于网络,根据自己对新知识的学习曲线重新整合了一下: 一,字形图标的定义 ...

  4. Web---字节输出流和字符输出流的共存问题、转发、重定向、请求包含知识点讲解

    本章博客的知识点: 1.字节输出流和字符输出流的共存问题 2.转发时,两个servlet都输出信息的问题 详细知识,见OneServlet和TwoServlet源代码中的注释 转发:传参,访问顺序(d ...

  5. Android学习笔记(十一)BroadcastReceiver动态注册、注销示例

    在上一篇博文中简单介绍了一下BroadcastReceiver的相关知识点,本篇举一个在代码中动态的注册.注销BroadcastReceiver的栗子. 1.首先创建一个MyReceiver并继承Br ...

  6. Bzoj 1975: [Sdoi2010]魔法猪学院 dijkstra,堆,A*,K短路

    1975: [Sdoi2010]魔法猪学院 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1357  Solved: 446[Submit][Statu ...

  7. ubuntu12.04下使用qemu模拟mips处理器安装debian

    注:ubuntu是不支持mips处理器的,只能在x86下安装运行第一步.安装qemu sudo apt-get install qemu qemu-system .执行 qemu-system-mip ...

  8. 使用alloctor模板来实现string类

    虽然以前做过更复杂的各种数据结构,不过那只是在看完c++prime7章后做的,没有考虑到类的拷贝体现出来是类值还是类指针,于是写了一些半成品类,不过那些主要是练数据结构,不想再改,于是就想办法模仿了下 ...

  9. HOWTO: Setup XCode 6.1 to work with OpenCV3 libraries

    HOWTO: Setup XCode 6.1 to work with OpenCV3 libraries Overview This post demonstrates how to setup y ...

  10. PHP 遍历文件目录

    /********************** 一个简单的目录递归函数 第一种实现办法:用dir返回对象 ***********************/ function tree($directo ...