The Xamarin XAML language teaches the template binding of a template-bound control template for a program template
To make it easy to change property values on controls in a control template, you can implement template binding functionality in a control template. Template bindings allow controls in a control template to bind data to public properties. You need to use TemplateBinding. It can bind the properties of a control in a control template to a bindable property on the parent of the target view that owns the control template.
Note: (1) TemplateBinding is similar to an existing binding, except that the source of TemplateBinding is always automatically set to the parent of the target view that owns the control template. (2) using templatebinding outside of the control template is not supported.
Example 14-5:controltemplatedemo the following will be based on Project Controltemplatedemo, which implements template binding functionality in a control template. The following steps are described:
(1) Open the MainPage.xaml file, write the code, and implement the definition of the Bindable property. The code is as follows:
- Namespace Controltemplatedemo
- {
- public partial class Mainpage:contentpage
- {
- bool Originaltemplate = true;
- ControlTemplate Tealtemplate;
- ControlTemplate Aquatemplate;
- public static readonly Bindableproperty Headertextproperty = bindableproperty.create ("HeaderText",
- typeof (String),
- typeof (MainPage),
- "Knowledge is power.");
- public static readonly Bindableproperty Footertextproperty = bindableproperty.create ("Footertext",
- typeof (String),
- typeof (MainPage),
- "Xamarin.froms XAML");
- Public MainPage ()
- {
- InitializeComponent ();
- ...//The instantiation of Tealtemplate and Aquatemplate objects is omitted here
- }
- Public string HeaderText
- {
- Get
- {
- Return (String) GetValue (Headertextproperty);
- }
- }
- public string Footertext
- {
- Get
- {
- Return (String) GetValue (Footertextproperty);
- }
- }
- ...//The implementation of the OnButtonClicked method is omitted here
- }
- }
(2) Open the App.xaml file, write the code, and implement the template binding function in the first built ControlTemplate. The code is as follows:
- <controltemplate x:key= "Tealtemplate" >
- <Grid>
- <Grid.RowDefinitions>
- <rowdefinition height= "0.1*"/>
- <rowdefinition height= "0.8*"/>
- <rowdefinition height= "0.1*"/>
- </Grid.RowDefinitions>
- <Grid.ColumnDefinitions>
- <columndefinition width= "0.05*"/>
- <columndefinition width= "0.95*"/>
- </Grid.ColumnDefinitions>
- <boxview grid.columnspan= "2"
- Color= "Teal"/>
- <label grid.column= "1"
- text= "{TemplateBinding parent.headertext}"
- Textcolor= "White"
- Fontsize= "18"
- verticaloptions= "Center"/>
- <contentpresenter grid.row= "1"
- grid.columnspan= "2"/>
- <boxview grid.row= "2"
- Grid.columnspan= "2"
- Color= "Teal"/>
- <label grid.row= "2"
- grid.column= "1"
- text= "{TemplateBinding parent.footertext}"
- Textcolor= "White"
- Fontsize= "18"
- verticaloptions= "Center"/>
- </Grid>
- </ControlTemplate>
In this code, we implemented the template binding feature for the Text property of the two label control, which we mentioned in the previous section that the property is bound to a bindable property on the parent of the target view that owns ControlTemplate by using a template binding. However, in our code, the template bindings are bound to Parent.headertext and Parent.footertext, not HeaderText and Footertext. This is because the bindable attribute in this code is defined on the grandparent level of the target view, not the parent.
Note: The source of the template bindings is always automatically set to the parent of the target view that owns the control template, which is the Contentview instance in this project. The template binding uses the Parent property to return a Contentview instance of the parents element, which is the Contentpage instance.
Run the program at this point and you will see the same effect as figure 14.12~14.14.
Xamarin XAML language teaches a template binding to a program template