zoukankan      html  css  js  c++  java
  • 转 绑定技巧 SL

     摘抄了 Jake Lin的博客 http://www.cnblogs.com/procoder/archive/2012/01/06/Windows-Phone-Databinding.html

     为什么Binding的这个属性叫做Path而不是Property呢?毕竟,Style类就有一个叫做Property的属性。为什么Binding却没有呢?

    简单的答案是Path能把多个属性名组合在一起使用。例如,假设Slider没有名称。如果你知道该Slider是ContentPanel元素的Children集合的第一个子元素,你可以间接地引用Slider:

    Text="{Binding ElementName=ContentPanel, Path=Children[0].Value}" 

    或者,使用可视化树更上一层的元素:

    Text="{Binding ElementName=LayoutRoot, Path=Children[1].Children[0].Value}" 

    Path的组成部件必须是通过点号连接的属性或者索引器(indexer)。

    this 绑定

      <UserControl x:Class="Petzold.Phone.Silverlight.BorderedText"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                 Name="this">
    
                <Border Background="{Binding ElementName=this, Path=Background}"
                BorderBrush="{Binding ElementName=this, Path=BorderBrush}"
                BorderThickness="{Binding ElementName=this, Path=BorderThickness}"
                CornerRadius="{Binding ElementName=this, Path=CornerRadius}"
                Padding="{Binding ElementName=this, Path=Padding}">
    
                    <TextBlock Text="{Binding ElementName=this, Path=Text}"
                       TextAlignment="{Binding ElementName=this, Path=TextAlignment}"
                       TextDecorations="{Binding ElementName=this, Path=TextDecorations}"
                       TextWrapping="{Binding ElementName=this, Path=TextWrapping}" />
                </Border>
            </UserControl>

     请注意根元素的命名:

    Name="this" 

    你可以把这个根元素设置成你想要的名称,但使用C#的关键字this是最常见的做法,因为在该XAML文件中,this是指BorderedText类当前的实例,因此可以使用this这一熟悉的概念。你可以通过this这一名字把BorderedText的属性与可视化树中元素的属性建立绑定关系。 

  • 相关阅读:
    斜率DP小结
    CF 1059 D Nature Reserve(double 精度问题)
    ZOJ 3747 Attack on Titans(DP+思维)
    BZOJ 1933 Bookcase 书柜的尺寸
    异或的一些性质
    (学习5 分治思想)最短对的问题
    (学习4)二分归并排序
    mac上git与github的联动
    (学习3)二分法与二叉查找树
    (学习2)Floyd和Dijkstra算法
  • 原文地址:https://www.cnblogs.com/moses/p/2419670.html
Copyright © 2011-2022 走看看