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.
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.
{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
© 2022 - 2024 — McMap. All rights reserved.
TemplateBinding
isOneWay
andTemplatedParent
isTwoWay
. – Lawn