WPF TemplateBinding 与 RelativeSource TemplatedParent [英] WPF TemplateBinding vs RelativeSource TemplatedParent

查看:38
本文介绍了WPF TemplateBinding 与 RelativeSource TemplatedParent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两个绑定之间的区别是什么:

What is the difference between these 2 bindings:

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

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

?

推荐答案

TemplateBinding 并不完全相同.MSDN 文档通常是由必须对单音节 SDE 进行软件功能测验的人编写的,因此细微差别不太正确.

TemplateBinding is not quite the same thing. MSDN docs are often written by people that have to quiz monosyllabic SDEs about software features, so the nuances are not quite right.

TemplateBindings 在编译时根据控件模板中指定的类型进行评估.这允许更快地实例化编译模板.只需在模板绑定中摸索名称,您就会看到编译器会标记它.

TemplateBindings are evaluated at compile time against the type specified in the control template. This allows for much faster instantiation of compiled templates. Just fumble the name in a templatebinding and you'll see that the compiler will flag it.

绑定标记在运行时解析.虽然执行速度较慢,但​​绑定将解析在模板声明的类型上不可见的属性名称.慢一点,我会指出它是相对的,因为绑定操作只占用很少的应用程序 CPU.如果您正在高速处理控制模板,您可能会注意到它.

The binding markup is resolved at runtime. While slower to execute, the binding will resolve property names that are not visible on the type declared by the template. By slower, I'll point out that its kind of relative since the binding operation takes very little of the application's cpu. If you were blasting control templates around at high speed you might notice it.

作为一种实践,当您可以但不要害怕绑定时,请使用模板绑定.

As a matter of practice use the TemplateBinding when you can but don't fear the Binding.

这篇关于WPF TemplateBinding 与 RelativeSource TemplatedParent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆