何时在WPF中使用TemplateBinding和TemplatedParent [英] When to use TemplateBinding and TemplatedParent in WPF

查看:592
本文介绍了何时在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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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