When to use TemplateBinding and TemplatedParent in WPF
Asked Answered
C

1

6

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.

Caligula answered 6/2, 2012 at 9:25 Comment(0)
G
18

{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

Garrison answered 6/2, 2012 at 14:5 Comment(1)
TemplateBinding is OneWay and TemplatedParent is TwoWay.Lawn

© 2022 - 2024 — McMap. All rights reserved.