Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use TemplateBinding and TemplatedParent in WPF

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

But my doubt is when to use TemplateBinding and TemplatedParent?

Thanks in advance.

like image 222
Syed Avatar asked Feb 06 '12 09:02

Syed


1 Answers

{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

like image 161
Rachel Avatar answered Nov 15 '22 06:11

Rachel