One of the things that can completely freak you out.
Lets start with facts:
1. While customize an application you might use something like this:
Or maybe this:
While this is on the ResourceDictionary of your app and about to styling your custom control, and MyProperty is an DependencyProperty on the logic part of the Control
so what the different between those two? And what the default choose should be?
By the way while most of programmers used to write
You can use the Short way as:
2. A full demonstration will be like this one:
And the Xaml part:
On purpose I used they both in my demo .
while running this code you will probably be aware that TemplateBinding doesn’t appear on your screen. Is it true?
what is it TemplateParent?(By MSDN documentation) TemplatedParent refers to the Control instance that the template is being applied to
Meaning when you using ControlTmplate this is the connection from the Xaml side to the Control source.
And what is TemplateBinding? (By MSDN documentation)
TemplateBinding is used for binding to the element properties within the template definition
Wait for this , go to your control and Change the MyProperty Definition type to String, now it works!!
Wait for this, go back to your Code and restore MyProperty to Int, than dd a Converter, name it as you wish and do nothing inside it,
I mean do something like:
It may looks as stupid thing to do, and no disagreement, but please give it a try,
Than connect it to your control Theme library.
well well.. Now, when we sure that there is a problem lets think..
It seems that they both do kind of same except that the internal converter of TemplateBinding is missing functionality of ToString .
While every FrameworkElement has a TemplatedParent which is deeply embedded in the Xaml Parser structural, and use it for regular Binding
TemplateBinding is just an Extension of MarkupExtension where designed to regard to original element property when creating a template for Control, so default built in values has the proper converters, but with personal binding targeting like in custom controls you must provide the converter.
Also It is totally wrong to say that TemplateBinding is shortcut for TemplateParent as I found around the web.
You use TemplateBinding in template to bind to a value on the control the template is applied to. A TemplateBinding is more efficient than a Binding but it has less functionality. Using a TemplateBinding is equivalent to using a Binding with the RelativeSource property set to RelativeSource.TemplatedParent.