1

I read this thread about template binding: What is the template binding vs binding? I want to know if the object the data template is binded to is a Template class, should I use template binding?

And I am currently using classic binding, how can I switch to use 'template binding'? Just replace 'Binding' to 'TemplateBinding'

My template class like this:

public class MyTemplateClass {

}

And I create an instance of this for the data template to binded to.

Community
  • 1
  • 1
n179911
  • 19,547
  • 46
  • 120
  • 162

1 Answers1

1

TemplateBinding has to do with the ControlTemplate, in brief it is a way to inject property into the template from the template user.

{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

ReeganLourduraj
  • 863
  • 6
  • 9