A bit on TemplateBinding and how to use it inside a ControlTemplate.
Introductio
Today I ' ll try to write a bit on and what to use TemplateBinding
it inside a ControlTemplate
. TemplateBinding
is a type of the binding used mainly for template scenarios. Here I am isn't going to write more on it theoretical aspect as what TemplateBinding
are, when to use, blah blah blah, as lot of Conte NT is readily available on the net. So, let's start quickly with the coding part:
First of all, let's create a new project using WPF template and place a button in it as below:
Now, what I am going to does is, and I am going to replace the this content template for this button. So, "in order" to "do", open up the Button tag and add Button.Template
markup tag with a new as ControlTemplate
:
Now as soon as you'll add ControlTemplate
tag, you'll notice that content of the button are gone and button is shown as a Tran Sparent rectangle. This have happened because here I told WPF to replace the default with the one ControlTemplate
which I defined. But at the this point, ControlTemplate
we are blank, so there are no visualization and we can see only a transparent rectangle.
Now go ahead and customize we by ControlTemplate
putting Ellipse
inside it as:
Now we can see, we get a visualization for a button in the form of ellipse. At the time, it works OK, but there is scenarios where this breaks is down struct
.
For example, let's increase the height button
of, from to 35
as 105
:
In the above image, you'll notice that button height is increased and the ellipse size is still the same, which are a bad UI design. And the reason is happening are, inside a ControlTemplate
, the height of an ellipse are hard coded. So, no matter, whatever height was set at parent (i.e., Button
), it would not get inherited to child control (i.e. Ellipse
).
So, now we had to fix the problem by using a special type of binding called TemplateBinding
inside ControlTemplate
. So, instead of hard coding the height, we'll use as TemplateBinding
shown below:
By setting TargetType
ControlTemplate
The property of, we is telling that and any property of Ellipse
Button
can is set to ellipse
. Now, whatever the height of button'll be, it'll automatically be the height of ellipse
also. ISN ' t it interesting?
Moving forward, let's do something more interesting with property of Fill
ellipse
.
In the above snippet, I am trying to set the property of the Fill
ellipse
using TemplateBinding
. But now the problem are, a button
doesn ' t has a property Fill
. So, there was no one-to-one mapping for property Fill
. Now, what does?
No need to worry that much because button
does has a property as Background
:
In the above image, you might has noticed that as soon ellipse
as's property Fill
Background
are set to, ellipse
becomes transparent As button
' s background. Now if we set button
' s property Background
Red
to, the same would be applied to ellipse
too.
So, one can understand how much magic we can does with TemplateBinding
.
Now, let's work a little bit on code cleanup.
ControlTemplate Inside Resource Dictionary
For better code readability, we'll move out our ControlTemplate
code and put it inside a resource dictionary as:
So, now we can see as earlier this whatever visual property button
for are set, all get applied to as well ellipse
.
Hope This tip is useful and gave you the basic idea on how to use TemplateBinding
.
WPF: Using TemplateBinding in ControlTemplate