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的属性与可视化树中元素的属性建立绑定关系。 

  • 相关阅读:
    八月份总结+项目总结
    缓存图片技术
    7月份总结
    【转】JavaScript 事件顺序:冒泡和捕获
    【转】UTF16和UTF8什么区别?
    【转】javascript和html中unicode编码和字符转义的详解
    【笔记】javascript权威指南-第六章-对象
    Delphi 中的MD5实现方法《转》
    DELPHI 代码块集合
    Delphi Access 表中查询日期时间提示出错的问题《转》
  • 原文地址:https://www.cnblogs.com/moses/p/2419670.html
Copyright © 2011-2022 走看看