WPF TemplateBinding 与 RelativeSource TemplatedParent
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1131222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF TemplateBinding vs RelativeSource TemplatedParent
提问by PaN1C_Showt1Me
What is the differencebetween 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>
?
?
回答by Grant BlahaErath
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.
TemplateBinding 并不完全相同。MSDN 文档通常由必须对单音节 SDE 进行软件功能测验的人编写,因此细微差别不太正确。
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.
TemplateBindings 在编译时根据控件模板中指定的类型进行评估。这允许更快地实例化编译模板。只需在模板绑定中摸索名称,您就会看到编译器会标记它。
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.
绑定标记在运行时解析。虽然执行速度较慢,但绑定将解析在模板声明的类型上不可见的属性名称。慢一点,我会指出它是相对的,因为绑定操作只占用很少的应用程序 CPU。如果您正在高速处理控制模板,您可能会注意到它。
As a matter of practice use the TemplateBinding when you can but don't fear the Binding.
作为实践问题,当您可以但不要害怕绑定时,请使用模板绑定。
回答by Paul Fischer
TemplateBinding- More limiting than using regular Binding
TemplateBinding- 比使用常规绑定更多的限制
- 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}
- 比 Binding 更有效,但功能较少
- 仅适用于 ControlTemplate 的可视化树
- 不适用于 Freezables 上的属性
- 在 ControlTemplate 的触发器中不起作用
- 提供设置属性的快捷方式(不是冗长的),例如 {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}"/>
- 尊重父属性
- 重置目标值以清除任何明确设置的值
- 示例:<Ellipse Fill="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Background}"/>
回答by Miroslav Nedyalkov
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).
还有一件事 - TemplateBindings 不允许值转换。例如,它们不允许您传递 Converter 并且不会自动将 int 转换为 string(这对于 Binding 来说是正常的)。
回答by Nir
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 是 Binding with TemplatedParent 的简写,但它没有公开 Binding 类的所有功能,例如,您无法从 TemplateBinding 控制 Binding.Mode。
回答by Yaz
I thought TemplateBinding does not support Freezable types (which includes brush objects). To get around the problem. One can make use of TemplatedParent
我认为 TemplateBinding 不支持 Freezable 类型(包括画笔对象)。为了解决这个问题。可以使用 TemplatedParent
回答by David Rogers
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
它们以类似的方式使用,但它们有一些区别。这是 TemplateBinding 文档的链接:http: //msdn.microsoft.com/en-us/library/ms742882.aspx