WPF TemplateBinding 与relativesource templatedparent
这两个绑定之间的区别是什么:
<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>
?
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>
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
TemplateBinding 并不完全相同。 MSDN 文档通常是由需要对单音节 SDE 进行有关软件功能的测试的人编写的,因此其中的细微差别并不完全正确。
TemplateBindings 在编译时根据控件模板中指定的类型进行评估。 这允许更快地实例化已编译的模板。 只需在模板绑定中摸索名称,您就会看到编译器会标记它。
绑定标记在运行时解析。 虽然执行速度较慢,但绑定将解析在模板声明的类型上不可见的属性名称。 通过较慢的速度,我将指出它是相对的,因为绑定操作占用应用程序的 CPU 很少。 如果您高速喷射控制模板,您可能会注意到它。
作为实践,尽可能使用 TemplateBinding,但不要害怕 Binding。
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 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.
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.
TemplateBinding - 更多限制比使用常规 Binding
常规绑定 - 没有上述 TemplateBinding
TemplateBinding - More limiting than using regular Binding
Regular Binding - Does not have above limitations of TemplateBinding
另一件事 - TemplateBindings 不允许值转换。 例如,它们不允许您传递转换器,并且不会自动将 int 转换为字符串(这对于绑定来说是正常的)。
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).
TemplateBinding 是 Binding with TemplatedParent 的简写,但它并没有公开 Binding 类的所有功能,例如您无法从 TemplateBinding 控制 Binding.Mode。
TemplateBinding 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 TemplateBinding.
我认为 TemplateBinding 不支持 Freezable 类型(包括画笔对象)。 为了解决这个问题。 可以利用 TemplatedParent
I thought TemplateBinding does not support Freezable types (which includes brush objects). To get around the problem. One can make use of TemplatedParent
它们的使用方式相似,但有一些差异。
以下是 TemplateBinding 文档的链接:
http://msdn.microsoft.com/en-us/library/ms742882。 ASPX
They are used in a similar way but they have a few differences.
Here is a link to the TemplateBinding documentation:
http://msdn.microsoft.com/en-us/library/ms742882.aspx