TemplateBinding is a lightweight version of the binding that loses many of the features of a mature version of the binding, such as inheriting content references (inheritence context referencing), RelativeSource references, as well as dynamic type conversions through the ivalueconverter/typeconverter mechanism. It supports only the frameworkelements generated by the template , and its data source reference points to the parent element in the template. The main purpose of TemplateBinding is to have a built-in template that binds the properties of templated elements, which in this case is much more efficient than the mature binding.
The following two binding effects are the same
<textblock text= "{TemplateBinding MyText}"/>
<textblock text= "{Binding path=mytext, Mode=oneway, Relativesource={relativesource templatedparent}"/>
In addition to the above, the binding supports the following bindings
A relative data source extension is required to bind the target object to an object that has a correlation with the target object itself. For example
<window x:class="Yingbao.Chapter2.RelativeEx.AppWin"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="Http://schemas.microsoft.com/winfx/2006/xaml"Title="Relative Binding"height=" -"Width=" -"> <stackpanel orientation="Horizontal"HorizontalAlignment="Center"> <textblock fontsize=" -"text="{BindingRelativesource={relativesource self}, path=fontsize}"/><textblock margin="10,1,1,5"Fontsize=" -"text="{BindingRelativesource={relativesource ancestortype={x:type StackPanel}}, Path=orientation}"/></StackPanel> </Window>
In this example, the author uses two relative data source extensions, the first TextBlock text is bound to its own font size, and the second TextBlock's text is bound to the orientation property of its parent node StackPanel.
TemplateBinding and binding