何时在WPF中使用TemplateBinding和TemplatedParent

编程入门 行业动态 更新时间:2024-10-25 08:17:53
本文介绍了何时在WPF中使用TemplateBinding和TemplatedParent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对TemplateBinding和TemplatedParent感到困惑.我也通过这个链接 WPF TemplateBinding与RelativeSource TemplatedParent

I have a confusion over TemplateBinding and TemplatedParent. I have gone through this link also WPF TemplateBinding vs RelativeSource TemplatedParent

但是我的疑问是何时使用TemplateBinding和TemplatedParent?

But my doubt is when to use TemplateBinding and TemplatedParent?

谢谢.

推荐答案

{TemplateBinding X}只是编写{Binding X, RelativeSource={RelativeSource TemplatedParent}}的快捷方式.

{TemplateBinding X} is simply a shortcut way of writing the {Binding X, RelativeSource={RelativeSource TemplatedParent}}.

它们的计算结果相同,尽管TemplateBinding是在编译时评估的,而RelativeSource TemplatedParent是在运行时评估的.

They evaluate to the same thing, although TemplateBinding is evaluated at compile-time while RelativeSource TemplatedParent is evaluated at run-time.

因为它是在编译时求值的,所以TemplateBinding的求值速度要快一些,但是如果它认为不存在绑定属性,它将抛出错误.如果您知道该属性存在,但编译器不知道该属性,则可以使用RelativeSource TemplatedParent,因为它是在运行时而不是编译时求值的.

Because it is evaluated at compile-time, TemplateBinding is a bit faster to evaluate however it will throw errors if it doesn't think the bound property exists. If you know the property exists but the compiler doesn't know about it, then you use RelativeSource TemplatedParent since it is evaluated at run-time instead of compile-time.

总而言之,请使用TemplateBinding,除非它给您一个错误并且您知道该属性存在.然后使用RelativeSource TemplatedParent

To summarize, use TemplateBinding unless it gives you an error and you know the property exists. Then use RelativeSource TemplatedParent

您所链接的问题的可接受答案包含有关两者之间区别的很好的总结

The accepted answer to the question you linked contains a pretty good summary on the differences between the two

更多推荐

何时在WPF中使用TemplateBinding和TemplatedParent

本文发布于:2023-11-03 06:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554465.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:WPF   TemplateBinding   TemplatedParent

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!