Hello,
there seems to be a problem with setting ControlTemplate via implicite Style from Application Resources. In this case content is designed like defined ControlTemplate, but all binded properties doesn't work.
Following example-code demonstrates explained behavior:
App.xaml:
<Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:ControlTemplateTest" x:Class="ControlTemplateTest.App"> <Application.Resources> <ResourceDictionary> <ControlTemplate x:Key="tileControlTemplate"> <Grid RowSpacing="0"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition/> </Grid.RowDefinitions> <BoxView Color="{TemplateBinding Path=BackgroundColor}" Grid.RowSpan="2" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/> <Grid BackgroundColor="#50000000" Padding="0,2"> <Label StyleId="tileHeader" Text="{TemplateBinding Path=Title}" FontAttributes="Bold" FontSize="Small" HorizontalOptions="Center"/> </Grid> <ContentPresenter Grid.Row="1" Padding="5"/> </Grid> </ControlTemplate> <Style TargetType="local:TileControl"> <Setter Property="ControlTemplate" Value="{StaticResource tileControlTemplate}"/> </Style> </ResourceDictionary> </Application.Resources> </Application>
DemoPage.xaml:
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:ControlTemplateTest" x:Class="ControlTemplateTest.DemoPage"> <ContentPage.BindingContext> <local:DemoPageViewModel/> </ContentPage.BindingContext> <StackLayout> <local:TileControl Title="Tile 1"> <StackLayout> <Label Text="Name:"/> <Label Text="{Binding Name}"/> </StackLayout> </local:TileControl> </StackLayout> </ContentPage>
Adding implicit style into DemoPage.Resources everything works fine.
Greetings
Posts
I just stumbled over this one I think. It appears the the
Content
maintained by theContentPresenter
does not derive theBindingContext
from the parent. I had to assign the binding context in code, which is bad hackery. If there's a better solution to this, I'll gladly see a correction here:(setting up a binding may do the trick, too btw)