WPF TemplateBinding 与relativesource templatedparent

发布于 2024-07-26 14:57:12 字数 547 浏览 6 评论 0原文

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

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

笑忘罢 2024-08-02 14:57:12

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.

苄①跕圉湢 2024-08-02 14:57:12

TemplateBinding - 更多限制比使用常规 Binding

  • 比 Binding 更高效,但功能较少
  • 只能在 ControlTemplate 的可视化树中工作
  • 不能与 Freezables 上的属性一起工作
  • 不能在 ControlTemplate 的触发器中工作
  • 提供设置属性的快捷方式(不那么冗长),例如{TemplateBinding targetProperty}

常规绑定 - 没有上述 TemplateBinding

  • 尊重父属性
  • 的限制,重置目标值以清除任何显式设置的值
  • 示例:

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}"/>
So尛奶瓶 2024-08-02 14:57:12

另一件事 - 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).

恋你朝朝暮暮 2024-08-02 14:57:12

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.

半步萧音过轻尘 2024-08-02 14:57:12

我认为 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

他不在意 2024-08-02 14:57:12

它们的使用方式相似,但有一些差异。
以下是 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文