Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the elements of a ControlTemplate in Xamarin Forms

I have a ControlTemplate defined in App.xaml. Now, I need to be able to handle certain UI events. In Visual Studio's XAML editor, if I attach a handler to an event, the handler is created in App.xaml.cs. However, I need to do this in pages that use this control template.

I'm thinking the only way is to iterate through the elements in the control template and find the right element and create handlers in pages. However, I'm not sure how to access the elements from the ControlTemplate object.

Does Xamarin provide a way to peek into the contents of a ControlTemplate?

like image 756
Mark13426 Avatar asked Aug 01 '16 03:08

Mark13426


1 Answers

Although this is a old topic but the actual Xamarin Forms API provides the following and works out of the box:

contentView.GetTemplateChild("nameOfTheControl")
like image 95
maex Avatar answered Sep 28 '22 05:09

maex