在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}}}">效果一样。

我们来看一看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="3">           <Grid.RowDefinitions>             <RowDefinition/>             <RowDefinition/>           </Grid.RowDefinitions>           <Grid.ColumnDefinitions>             <ColumnDefinition/>             <ColumnDefinition/>           </Grid.ColumnDefinitions>           <Border BorderThickness="1" BorderBrush="Gray"                   Margin="2" Grid.RowSpan="2"                   VerticalAlignment="Center" HorizontalAlignment="Stretch">

<TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value}"                        Width="60" TextAlignment="Right" Padding="5"/>           </Border>         </Grid>       </ControlTemplate>     </Setter.Value>   </Setter> </Style>

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

{TemplateBinding Path=PathToProperty}

例子:

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

<转>WPF 中的绑定的更多相关文章

  1. 封装:WPF中可以绑定的BindPassWord控件

    原文:封装:WPF中可以绑定的BindPassWord控件 一.目的:本身自带的PassWord不支持绑定 二.Xaml部分 <UserControl x:Class="HeBianG ...

  2. WPF 中双向绑定通知机制之ObservableCollection使用

    msdn中   ObservableCollection<T> 类    表示一个动态数据集合,在添加项.移除项或刷新整个列表时,此集合将提供通知. 在许多情况下,所使用的数据是对象的集合 ...

  3. WPF中RadioButton绑定数据的正确方法

    RadioButton一般用于单选的时候,也就是从一组值中选择一个值. 比如性别有“男”和“女”两种取值,而对于一个员工的实例来说,性别的取值要么是男,要么是女. 这种时候一般就会用到RadioBut ...

  4. ComboBox在WPF中的绑定示例:绑定项、集合、转换,及其源代码

    示例1.Selector(基类) 的示例Controls/SelectionControl/SelectorDemo.xaml <Page x:Class="Windows10.Con ...

  5. WPF中Grid绑定DataTable数据。

    1.首先引用DocumentFormat.OpenXml.dll 2.然后新建一个OpenExcelHelper类,将Excel转化为Datatable. /// <summary>    ...

  6. WPF 中的绑定方式

    1.元素间的绑定 xaml方式 <Slider Name="slider1" Value="20"/>        <TextBlock T ...

  7. WPF中ItemsControl绑定到Google ProtocolBuffer的结构体时的性能问题

    背景: 最近遇到一个DataGrid的性能问题:里面大概有4000个数据, 绑定的ItemSource的类也只有一层数据,即简单的List(里面每个是Protocol Buffer自动产生的一个类,1 ...

  8. wpf中datagrid绑定数据源发生改变

    1.若datagrid绑定的数据源是同一个的话,即使里面的数据不同.页面也不会刷新,则需要重置数据源,再绑定.处理如下: datagrid1.ItemsSource=ListModule; 若List ...

  9. WPF中ComboBox绑定数据库自动读取产生数据

    前台端 <ComboBox HorizontalAlignment="Name="cmb_SSBM" DisplayMemberPath="NAME&qu ...

随机推荐

  1. Vysor

    官网:http://www.vysor.io/ Vysor用 PC远程控制投影安卓手机/平板工具 Vysor 是一个免费的google浏览器插件. 它可以让你在pc上控制你的Android手机.平板等 ...

  2. 密度峰值聚类算法(DPC)

    密度峰值聚类算法(DPC) 凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 1. 简介 基于密度峰值的聚类算法全称为基于快速搜索和发现密度峰值的聚类算法(cl ...

  3. nginx默认配置文件解释

    nginx默认配置文件 nginx.conf 介绍: 全局配置 user  nginx; 设置nginx服务的系统使用用户 worker_processes  1; 工作进程数(建议和CPU核心数保持 ...

  4. matlib调用python时转py格式为matlib格式

    因为需要,我用matlib调用python代码. 调用成功但是遇到问题 如下 调用完的结果为python格式   (py.list,py.xx) matlib根本不能用 查了半天一个能解决的方法都没 ...

  5. Linux上发布E卡通项目

    Linux上发布E卡通项目 使用的命令 ps -ef | grep java kill -9 22314 nohup java -jar smartcard-ms-0.0.1-SNAPSHOT.jar ...

  6. Apache 监听 ipv4

    查看端口信息 ifconfig 发现只有 ipv6 的 80 端口被监听,ipv4 的 80 端口没有被监听 因此通过 ipv4 无法访问 tcp6 0 0 :::80 :::* LISTEN 221 ...

  7. jenkins配置自动部署java程序

    任务背景 该项目包含4个程序包:a.jar,b.jar,c.jar,d.jar,每次启动前需要修改程序中的配置文件(修改数据源配置),然后按照先后顺序启动. 任务目标 1.利用jenkins拉取代码, ...

  8. A1010 Radix (25 分)

    一.技术总结 首先得写一个进制转换函数convert(),函数输入参数是字符串str和需要转化的进制(使用long long数据类型).函数内部知识,使用函数迭代器,即auto it = n.rbeg ...

  9. 奇安信集团笔试题:二叉树的最近公共祖先(leetcode236),杀死进程(leetcode582)

    1. 二叉树最近公共祖先     奇安信集团 2020校招 服务端开发-应用开发方向在线考试 编程题|20分2/2 寻祖问宗 时间限制:C/C++语言 1000MS:其他语言 3000MS 内存限制: ...

  10. 能耗监测平台GPRS通讯服务器的架构设计

    在这个文章里面我将用一个实际的案例来分享如何来构建一个能够接受3000+个连接的GPRS通讯服务器软件.在这里,我将分享GPRS通讯服务器设计过程中面临的问题,分享通讯协议的设计,分享基于异步事件的设 ...