Forum Xamarin.Forms


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Problem with ControlTemplate and DataBinding set via Style


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:


<Application xmlns="" xmlns:x=""
             xmlns:local="clr-namespace:ControlTemplateTest" x:Class="ControlTemplateTest.App">
            <ControlTemplate x:Key="tileControlTemplate">
                <Grid RowSpacing="0">
                        <RowDefinition Height="Auto"/>
                    <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"/>
                    <ContentPresenter Grid.Row="1" Padding="5"/>
            <Style TargetType="local:TileControl">
                <Setter Property="ControlTemplate" Value="{StaticResource tileControlTemplate}"/>


<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns=""
        <local:TileControl Title="Tile 1">
                <Label Text="Name:"/>
                <Label Text="{Binding Name}"/>

Adding implicit style into DemoPage.Resources everything works fine.



  • PhilippSumiPhilippSumi USMember ✭✭✭

    I just stumbled over this one I think. It appears the the Content maintained by the ContentPresenterdoes not derive the BindingContext 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)

    public class CardView : ContentView
        /// HACK Explicitly updates the binding context of the Content (doesn't seem to
        /// be derived implicitly.
        protected override void OnBindingContextChanged()
            if (Content != null)
                Content.BindingContext = BindingContext;
Sign In or Register to comment.