首页 > Windows开发 > 详细

[WPF系列-高级TemplateBinding vs RelativeSource TemplatedParent]

时间:2014-12-17 18:20:56      阅读:1392      评论:0      收藏:0      [点我收藏+]

What is the difference between these 2 bindings:

<ControlTemplate TargetType="{x:Type Button}">
   <Border BorderBrush="{TemplateBinding Property=Background}">
      <ContentPresenter />
   </Border>
</ControlTemplate>

and

<ControlTemplate TargetType="{x:Type Button}">
   <Border BorderBrush="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}">
      <ContentPresenter />
   </Border>
</ControlTemplate>

TemplateBinding - More limiting than using regular Binding

  • More efficient than a Binding but it has less functionality
  • Only works inside a ControlTemplate‘s visual tree
  • Doesn‘t work with properties on Freezables
  • Doesn‘t work within a ControlTemplate‘s Trigger
  • Provides a shortcut in setting properties(not as verbose),e.g. {TemplateBinding targetProperty}

Regular Binding - Does not have above limitations of TemplateBinding

  • Respects Parent Properties
  • Resets Target Values to clear out any explicitly set values
  • Example: <Ellipse Fill="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Background}"/>

 

One more thing - TemplateBindings don‘t allow value converting. They don‘t allow you to pass a Converter and don‘t automatically convert int to string for example (which is normal for a Binding).

 

TempleteBinding is a shorthand for Binding with TemplatedParent but it does not expose all the capabilities of the Binding class, for example you can‘t control Binding.Mode from TempleteBinding.

 

参考

WPF TemplateBinding vs RelativeSource TemplatedParent

[WPF系列-高级TemplateBinding vs RelativeSource TemplatedParent]

原文:http://www.cnblogs.com/HQFZ/p/4169874.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!