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

先写到这,下篇继续关注Binding中ElementName,RelativeSource,Source的相同点和区别。

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

接上篇,

我们来看一看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>

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

[转]WPF中Binding的技巧的更多相关文章

  1. 【转】WPF中Binding的技巧(一)

    WPF中Binding的技巧(一)   在WPF应用的开发过程中Binding是一个非常重要的部分. 在实际开发过程中Binding的不同种写法达到的效果相同但事实是存在很大区别的. 这里将实际中碰到 ...

  2. WPF中Binding使用StringFormat格式化字符串方法

    原文:WPF中Binding使用StringFormat格式化字符串方法 货币格式 <TextBlock Text="{Binding Price, StringFormat={}{0 ...

  3. 整理:WPF中Binding的几种写法

    原文:整理:WPF中Binding的几种写法 目的:整理WPF中Bind的写法 <!--绑定到DataContext--> <Button Content="{Bindin ...

  4. WPF之Binding深入探讨

    原文:http://blog.csdn.net/fwj380891124/article/details/8107646 1,Data Binding在WPF中的地位 程序的本质是数据+算法.数据会在 ...

  5. WPF的Binding功能解析

    1,Data Binding在WPF中的地位 程序的本质是数据+算法.数据会在存储.逻辑和界面三层之间流通,所以站在数据的角度上来看,这三层都很重要.但算法在3层中的分布是不均匀的,对于一个3层结构的 ...

  6. WPF之Binding的使用

    引出: 在WPF中Binding可以比作数据的桥梁,桥梁的两端分别是Binding的源(Source)和目标(Target).一般情况下,Binding源是逻辑层对象,Binding目标是UI层的控件 ...

  7. WPF之Binding深入探讨--Darren

    1,Data Binding在WPF中的地位 程序的本质是数据+算法.数据会在存储.逻辑和界面三层之间流通,所以站在数据的角度上来看,这三层都很重要.但算法在3层中的分布是不均匀的,对于一个3层结构的 ...

  8. 深入浅出WPF之Binding的使用(一)

    在WPF中Binding可以比作数据的桥梁,桥梁的两端分别是Binding的源(Source)和目标(Target).一般情况下,Binding源是逻辑层对象,Binding目标是UI层的控件对象:这 ...

  9. WPF之Binding【转】

    WPF之Binding[转] 看到WPF如此之炫,也想用用,可是一点也不会呀. 从需求谈起吧: 首先可能要做一个很炫的界面.见MaterialDesignInXAMLToolKit. 那,最主要的呢, ...

随机推荐

  1. Codeforces Round #563 (Div. 2) F. Ehab and the Big Finale

    后续: 点分治标程 使用father数组 比使用vis数组优秀(不需要对vis初始化) https://codeforces.com/problemset/problem/1174/F https:/ ...

  2. 概率dp——逆推期望+循环迭代zoj3329

    首先要推出dp[i]的期望方程,会发现每一项都和dp[0]相关, 那我们将dp[i]设为和dp[0]有关的式子dp[i]=a[i]*dp[0]+b[i],然后再回代到原来的期望方程里 然后进行整理,可 ...

  3. C# GZipStream 压缩 解压

    关于GZipStream压缩解压,网上找了很多资料,完整的不多,要么是针对字符串压缩解压缩的,要么只实现了针对单个文件的压缩解压缩,还有的不支持子文件夹的压缩,实用性都不是很大. 以下整理了压缩解压缩 ...

  4. 二分查找总结及部分Lintcode题目分析 1

    进行二分查找课程回顾与总结,包括以下几个方面,二分法的模板总结和解题思路.应用. 二分法模板总结classical binary search: 1. 必须要做的排除极端情况,也就是数组(用A表示)不 ...

  5. SpringAOP中的aop:config标签

    我们使用Spring的AOP功能的时候发现,我们使用普通的配置方式的时候,我们无法精确的确定将切面类中的哪个方法切入到哪个切入点上, 所以我们可以使用aop的专用标签来完成相关的配置.其中主要表现是使 ...

  6. demjson处理json数据

    因为json数据不规范出现了以下问题: json.decoder.JSONDecodeError: Expecting property name enclosed in double quo 网上查 ...

  7. python代码打包成exe文件

    1.准备工作 1.安装pywin32.pyinstaller 2.准备好ico文件 找一个png格式的图片,使用png2ico脚本生成包含以下6个尺寸的ico文件:128×128 64×64 48×4 ...

  8. Markdown语法--整理

    Markdown基本语法 [TOC] 优点: 1.因为是纯文本,所以只要支持Markdown的地方都能获得一样的编辑效果,可以让作者摆脱排版的困扰,专心写作. 2.操作简单.比如:word编辑时标记个 ...

  9. Leetcode931. Minimum Falling Path Sum下降路径最小和

    给定一个方形整数数组 A,我们想要得到通过 A 的下降路径的最小和. 下降路径可以从第一行中的任何元素开始,并从每一行中选择一个元素.在下一行选择的元素和当前行所选元素最多相隔一列. 示例: 输入:[ ...

  10. JS 标签页切换

    一 <!DOCTYPE html><html><head><meta charset="utf-8"/><title>自 ...