When to use TemplateBinding and TemplatedParent in WPF

安稳与你 提交于 2019-12-05 06:59:23
Rachel

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

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

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!