zoukankan      html  css  js  c++  java
  • What is the difference between Binding and TemplateBinding?

    Binding provides much more flexibility than TemplateBinding, but it’s more costly. TemplateBinding is limited to one scenario but very efficient in what it does.

    Everytime you want to bind to a property of an element from within its template, you should consider using a TemplateBinding. For example, consider the following scenario:

        <Window x:Class=”CommonQuestions.Window1″
         xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
         xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
         Title=”CommonQuestions” Height=”300″ Width=”300″
         >
            <Window.Resources>
                <ControlTemplate TargetType=”{x:Type Button}” x:Key=”buttonTemplate”>
                    <Border BorderBrush=”{TemplateBinding Property=Background}” BorderThickness=”3″ >
                        <ContentPresenter Margin=”10″/>
                    </Border>
                </ControlTemplate>
            </Window.Resources>
            <StackPanel Margin=”20″>
                <Button Template=”{StaticResource buttonTemplate}” HorizontalAlignment=”Center” Background=”SteelBlue”>Hello</Button>
            </StackPanel>
        </Window>

    In this case, I want the BorderBrush property of Border to be whatever color is specified in the Button’s Background. This is exactly the scenario that TemplateBinding is optimized for. Instead of the TemplateBinding in this snippet, you could use the following Binding: {Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}. The resulting behavior would be the same, but this is not as efficient, and it’s quite a bit more complex to write, so TemplateBinding is the preferred approach. TemplateBinding also allows you to specify a Converter and a ConverterParameter, which increases the scope of the supported scenarios quite a bit. However, a TemplateBinding can only transfer data in one direction: from the templated parent to the element with the TemplateBinding. If you need to transfer data in the opposite direction or both ways, a Binding with RelativeSource of TemplatedParent is your only option. For example, interaction with a TextBox or Slider within a template will only change a property on the templated parent if you use a two-way Binding.

    In summary: if you want a one-way binding from within a template to a property of its templated parent, use TemplateBinding. For all other scenarios, or for extra flexibility in this last scenario, use Binding.

  • 相关阅读:
    SpringMVC + Spring + MyBatis 学习笔记:遭遇order by 排序问题
    SpringMVC + Spring + MyBatis 学习笔记:SpringMVC和Spring一同工作的时候,AOP事务管理不起作用的解决方法
    SpringMVC + Spring + MyBatis 学习笔记:提交数据遭遇基础类型和日期类型报400错误解决方法
    SpringMVC + Spring + MyBatis 学习笔记:为MyBatis增加打印SQL功能 (最简化配置)
    [转]大部分人努力程度之低,根本轮不到拼天赋
    String内存陷阱简介
    同为程序员 为什么我的工资最低
    在程序员的眼里,用户是这样使用他们开发的软件的
    POI怎么和项目结合起来使用
    uploadify
  • 原文地址:https://www.cnblogs.com/liangouyang/p/1311748.html
Copyright © 2011-2022 走看看