Binding 中 Elementname,Source,RelativeSource 三种绑定的方式
在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。
效果:
- <StackPanel DataContext="abc">
- <Label Content="{Binding}"></Label>
- <Label Content="{Binding RelativeSource={RelativeSource Self},Path=DataContext}"></Label>
- </StackPanel>
- <StackPanel DataContext="abc">
- <Label Content="{Binding}"></Label>
- <Label DataContext="def" Content="{Binding RelativeSource={RelativeSource Self},Path=DataContext}"></Label>
- </StackPanel>
2.在Template的Trigger中改变Template中某个样式控件的属性
- <Style TargetType="{x:Type Button}">
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="{x:Type Button}">
- <Border>
- <Label x:Name="PART_Label" Content="{TemplateBinding ContentA}" />
- </Border>
- <ControlTemplate.Triggers>
- <Trigger Property="IsChecked" Value="True">
- 注: <Setter TargetName="PART_Label" Property="Content" Value="{Binding Path=ContentB, RelativeSource={RelativeSource TemplatedParent}}" />
- </Trigger>
- </ControlTemplate.Triggers>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </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来进行绑定:
例子:
- <Window x:Name="MainWindow">
- <Grid>
- <Button Background=”{Binding ElementName=MainWindow, Path=Background}”/>
- </Grid>
- </Window>
- 效果等同于
- <Window>
- <Grid>
- <Button Background=”{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window},Path=Background}”/>
- </Grid>
- </Window>
区别:
ElementName属性用于引用一个UI对象的名称,其的作用域在同一XAML文件内,不能引用另一XAML文件的某个Ui元素名。
2.Source属性用于指定对象绑定路径的引用。 其特点是:Source属性通常用于绑定设置的对象时,是已知的。
- <Window x:Name="MainWindow">
- <Grid>
- <Button Background=”{Binding Source={StaticResource ButtonStyle}}”/>
- </Grid>
- </Window>
3.RelativeSource
在不确定绑定的Source时,但知道与绑定对象两者相对关系时就需要使用RelativeSource,这也是RelativeSource 与ElementName和Source的最大区别。
RelativeSource 的三种典型用法:
/1.UI元素的一个属性绑定在自身的另一个属性上
- <Label Background = {Binding Path=Forgroud, RelativeSource={RelativeSource Self}} />
/2.UI元素的一个属性绑定在某个父元素的属性上
- <Grid>
- <Label Background = {Binding Path=Background, RelativeSource={RelativeSource AncestorType={x:Type Grid}}}/>
- </Grid>
/3.Template中的元素的属性绑定在Template使用者元素的属性上
- {Binding Path=PathToProperty, RelativeSource={RelativeSource TemplatedParent}}
例子:
- <Style TargetType="{x:Type local:NumericUpDown}">
- <Setter Property="HorizontalAlignment" Value="Center"/>
- <Setter Property="VerticalAlignment" Value="Center"/>
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="{x:Type local:NumericUpDown}">
- <Grid Margin="">
- <Grid.RowDefinitions>
- <RowDefinition/>
- <RowDefinition/>
- </Grid.RowDefinitions>
- <Grid.ColumnDefinitions>
- <ColumnDefinition/>
- <ColumnDefinition/>
- </Grid.ColumnDefinitions>
- <Border BorderThickness="" BorderBrush="Gray"
- Margin="" Grid.RowSpan=""
- VerticalAlignment="Center" HorizontalAlignment="Stretch">
- <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value}"
- Width="" TextAlignment="Right" Padding=""/>
- </Border>
- </Grid>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
利用TemplateBinding 绑定模板与原对象之间的属性
{TemplateBinding Path=PathToProperty}
例子:
- <ControlTemplate TargetType="{x:Type Button}" x:Key="buttonTemp">
- <Border BorderThickness="" Background="{TemplateBinding Foreground}">
- <TextBlock Foreground="{TemplateBinding Background}"/>
- </Border>
- </ControlTemplate>
转载时,请注明本文来源:www.cnblogs.com/tmywu
Binding 中 Elementname,Source,RelativeSource 三种绑定的方式的更多相关文章
- javascript中var let const三种变量声明方式
javascript中var let const三种变量声明方式 1.var ①var表示声明了一个变量,并且可以同时初始化该变量. ②使用var语句声明的变量的作用域是当前执行位置的上下文:一个函 ...
- spring boot:thymeleaf模板中insert/include/replace三种引用fragment方式的区别(spring boot 2.3.3)
一,thymeleaf模板中insert/include/replace三种引用fragment方式的区别 insert: 把整个fragment(包括fragment的节点tag)插入到当前节点内部 ...
- 后台将数据传回前台的三种绑定的方式(Model,Map.ModelAndView)
//方式1:通过model 将数据绑定 @RequestMapping(value = "findByIdModel", method = RequestMethod.GET) p ...
- js this详解,事件的三种绑定方式
this,当前触发事件的标签 在绑定事件中的三种用法: a. 直接HTML中的标签里绑定 onclick="fun1()"; b. 先获取Dom对象,然后利用dom对象在js里绑定 ...
- Dom事件的三种绑定方式
1.事件 2. onclick, onblur, onfocus, 需求:请写出一个行为,样式,结构,相分离的页面. JS, CSS, HTML, 示例1,行为结构样式粘到一起的页面: & ...
- jQuery中detach&&remove&&empty三种方法的区别
jQuery中empty&&remove&&detach三种方法的区别 empty():移除指定元素内部的所有内容,但不包括它本身 remove():移除指定元素内部的 ...
- Hibernate中Java对象的三种状态
Hibernate中Java对象的三种 ...
- .NET中的三种接口实现方式
摘自:http://www.cnblogs.com/zhangronghua/archive/2009/11/25/1610713.html 一般来说.NET提供了三种不同的接口实现方式,分别为隐式接 ...
- C语言中最常用的三种输入输出函数scanf()、printf()、getchar()和putchar()
本文给大家介绍C语言中最常用的三种输入输出函数scanf().printf().getchar()和putchar(). 一.scanf()函数格式化输入函数scanf()的功能是从键盘上输入数据,该 ...
随机推荐
- SQLite 对中文路径的支持(用到了StringToWideChar和Utf8Encode在D7的System单元中自带)
最近用SQLITE作为数据库,发现,如果直接传递带中文路径或文件名的数据库,会导致无法打开数据库的情况.看了一下SQLITE的源码,才发现,原来SQLITE中是用UTF8编码进行文件打开操作的. 所以 ...
- block的是发送信号的线程,又不是处理槽函数的线程
请问UI线程给子线程发信号,应该用哪种连接方式? 如果子线程正在执行一个函数,我发射信号去执行子线程的另一个函数,那么此时子线程到底会执行什么呢? 用信号量做的同步.第一把信号槽的事件丢到线程的事件队 ...
- Bootstrap框架中的字形图标的理解
最近项目中准备使用 Bootstrap 框架,看中了Ace Admin 这套皮肤,看其代码的时候,发现使用了字形图标.下面内容来源于网络,根据自己对新知识的学习曲线重新整合了一下: 一,字形图标的定义 ...
- Web---字节输出流和字符输出流的共存问题、转发、重定向、请求包含知识点讲解
本章博客的知识点: 1.字节输出流和字符输出流的共存问题 2.转发时,两个servlet都输出信息的问题 详细知识,见OneServlet和TwoServlet源代码中的注释 转发:传参,访问顺序(d ...
- Android学习笔记(十一)BroadcastReceiver动态注册、注销示例
在上一篇博文中简单介绍了一下BroadcastReceiver的相关知识点,本篇举一个在代码中动态的注册.注销BroadcastReceiver的栗子. 1.首先创建一个MyReceiver并继承Br ...
- Bzoj 1975: [Sdoi2010]魔法猪学院 dijkstra,堆,A*,K短路
1975: [Sdoi2010]魔法猪学院 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1357 Solved: 446[Submit][Statu ...
- ubuntu12.04下使用qemu模拟mips处理器安装debian
注:ubuntu是不支持mips处理器的,只能在x86下安装运行第一步.安装qemu sudo apt-get install qemu qemu-system .执行 qemu-system-mip ...
- 使用alloctor模板来实现string类
虽然以前做过更复杂的各种数据结构,不过那只是在看完c++prime7章后做的,没有考虑到类的拷贝体现出来是类值还是类指针,于是写了一些半成品类,不过那些主要是练数据结构,不想再改,于是就想办法模仿了下 ...
- 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 ...
- PHP 遍历文件目录
/********************** 一个简单的目录递归函数 第一种实现办法:用dir返回对象 ***********************/ function tree($directo ...