xaml - How can i capture the event of a tapped item (located in a template) from the base class? -


i have base grid

<grid grid.row="1" grid.column="1" x:name="gridname">             <stacklayout orientation="vertical">                 <art:gridoptionsview itemssource="{binding items}" >                     <art:gridoptionsview.itemtemplate>                         <datatemplate>                             <uikit:dashboarditemtemplate />                         </datatemplate>                     </art:gridoptionsview.itemtemplate>                 </art:gridoptionsview>             </stacklayout>         </grid> 

which uses following dashboarditemtemplate

<?xml version="1.0" encoding="utf-8"?> <contentview xmlns="http://xamarin.com/schemas/2014/forms"          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"          backgroundcolor="white">     <contentview.content>         <grid padding="0">             <stacklayout verticaloptions="center" horizontaloptions="center" orientation="vertical" spacing="10">                 <grid>                     <label text="" style="{staticresource fonticon}" horizontaltextalignment="center" opacity="1" fontsize="130" textcolor="{binding backgroundcolor}" verticaloptions="center" horizontaloptions="center" isvisible="{binding source={x:reference root}, path=showiconcoloredcirclebackground}" />                     <label text="{binding icon}" style="{staticresource fonticon}" opacity="1" textcolor="white" verticaloptions="center" horizontaloptions="center" />                 </grid>                 <label text="{binding name}" textcolor="{binding source={x:reference root}, path=textcolor}" fontsize="14" horizontaltextalignment="center">                 </label>             </stacklayout>         </grid>     </contentview.content>     <contentview.gesturerecognizers>         <tapgesturerecognizer tapped="onwidgettapped" />     </contentview.gesturerecognizers> </contentview> 

how can capture "onwidgettapped" event on base xaml class?

i custom bindable property parentbindingcontext in template:

public class mytemplate : contentpage {     public static bindableproperty parentbindingcontextproperty = bindableproperty.create(nameof(parentbindingcontext),          typeof(object), typeof(basepagetemplate));       public object parentbindingcontext     {         { return getvalue(parentbindingcontextproperty); }         set { setvalue(parentbindingcontextproperty, value); }     } } 

and in page (which contains template) set parentbindingcontext:

<datatemplate>     <template:mytemplate parentbindingcontext="{binding bindingcontext, source={x:reference name=mypagename}}" /> </datatemplate> 

with can access full bindingcontext of page in template. following example of command shows how template can bind command mycommand, in bindingcontext of page:

command="{binding parentbindingcontext.mycommand, source={x:reference name=mytemplatepagename}}" 

but presupposes page has bindingcontext behind (like viewmodel). viewmodel contains "global" commands whole page. these commands (or methods) can accessed template, because know bindingcontext of page.


Comments