zoukankan      html  css  js  c++  java
  • WPF中的Binding技巧(二)

       接上篇,

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

       

      作者: 淘米部落

          mail:tommywu23@126.com

  • 相关阅读:
    在移动端实现常按事件
    HBuilder的一些常用快捷键
    如何在vue里面调用高德地图
    js中获取当前时间
    vux使用方法
    模糊搜索的用法
    Vue-随笔小记
    常用到的一些事件
    java对接东华医疗数据库
    tomcat设置jdk路径
  • 原文地址:https://www.cnblogs.com/tmywu/p/2624798.html
Copyright © 2011-2022 走看看