c# - ControlTemplate with multiple content presenters. How to bind the Content of a ContentPresenter to the value of a DataTemplate in Xamarin Forms -
i have custom control xamarin forms looks one:
public class cardview : frame { public static readonly bindableproperty headerdatatemplateproperty = bindableproperty.create("headerdatatemplate", typeof(datatemplate), typeof(headercardview), null, bindingmode.twoway); public static readonly bindableproperty bodydatatemplateproperty = bindableproperty.create("bodydatatemplate", typeof(datatemplate), typeof(headercardview)); public datatemplate headerdatatemplate { => (datatemplate)getvalue(headerdatatemplateproperty); set => setvalue(headerdatatemplateproperty, value); } public datatemplate bodydatatemplate { => (datatemplate)getvalue(bodydatatemplateproperty); set => setvalue(bodydatatemplateproperty, value); } public cardview() { if (device.runtimeplatform.equals("ios")) { hasshadow = false; outlinecolor = color.transparent; backgroundcolor = color.transparent; } } }
the idea create "card" control can define 2 datatemplates heading , body.
so define controltemplate use 2 content presenters:
<controltemplate x:key="headercardcontroltemplate"> <stacklayout> <contentview padding="5" backgroundcolor="#a6192e"> <contentpresenter content="{templatebinding parent.headerdatatemplate, mode=twoway}"></contentpresenter> </contentview> <contentview> <contentpresenter content="{templatebinding parent.bodydatatemplate}"></contentpresenter> </contentview> </stacklayout> </controltemplate>
and use this:
<controls:headercardview controltemplate="{staticresource headercardcontroltemplate}"> <controls:headercardview.headerdatatemplate> <datatemplate> <grid> <grid.columndefinitions> <columndefinition></columndefinition> <columndefinition></columndefinition> </grid.columndefinitions> <label fontattributes="bold" text="{binding value}" grid.row="0" grid.column="0"></label> <label fontattributes="bold, italic" text="{binding type}" horizontaloptions="end" grid.row="0" grid.column="1"></label> </grid> </datatemplate> </controls:headercardview.headerdatatemplate> </controls:headercardview>
however contentpresenter not showing headerdatatemplate suppose grid.
any ideas of i'm missing or doing wrong?
Comments
Post a Comment