I have tried to follow the following example but with no luck:
https://developer.xamarin.com/guides/xamarin-forms/templates/control-templates/template-binding/
I have created a controltemplate that I want to bind with commands in my viewmodel.
ViewModel
public ICommand VoteYesCommand { get; set; } public ICommand VoteNoCommand { get; set; }
Xaml Page
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="VoteKMD.Features.PollPage.Page.PollPage" x:Name="PollPageReferenceName" xmlns:converters="clr-namespace:VoteKMD.Converters;assembly=VoteKMD" ControlTemplate="{StaticResource FooterTemplate}"> <StackLayout> <Label IsVisible="{Binding DescriptionLabelIsVisible}" HorizontalOptions="Center" FontSize="Small" Text="{Binding SelectedPollItem.Description}" /> </StackLayout> </ContentPage>
ControlTemplate
<Application.Resources> <ResourceDictionary> <ControlTemplate x:Key="FooterTemplate"> <StackLayout Orientation="Vertical" AbsoluteLayout.LayoutBounds="1,1,1,1" AbsoluteLayout.LayoutFlags="All"> <StackLayout VerticalOptions="StartAndExpand"> <ContentPresenter /> </StackLayout> <StackLayout VerticalOptions="End"> <Button Text="Yes" Command="{TemplateBinding VoteYesCommand}"/> <Button Text="No" Command="{TemplateBinding VoteNoCommand}"/> </StackLayout> </StackLayout> </ControlTemplate> </ResourceDictionary> </Application.Resources>
The application and the template works fine but the bindings on the two buttons in the controltemplate doesnt work. I have set breakpoints inside the two commands and the methods aren't reached.
I have tried to do {TemplateBinding Parent.VoteNoCommand}
as the example suggests but the "Parent" type is unknown.
Using "ContentView" inside the ContentPage doesnt seem to work either.
Also the linked example is 3 months old and I am running forms 2.2.0.45. Is there something new to this that they haven't documented?