0

In Xamarin Forms, I defined a custom data template like so:

<DataTemplate x:Key="MyControlDataTemplate">
     <ViewCell>
        <controls:MyControl/>
    </ViewCell>
</DataTemplate>

This snippet belongs to a ListView in MainPage.xaml which has its binding context set to MainPageViewModel.cs

Inside MyControl, I want to bind to a property of MainPageViewModel.

<Label.GestureRecognizers>
    <TapGestureRecognizer Command="{set binding here to MainPageViewModel property}" />
</Label.GestureRecognizers>

MainPageViewModel has this command property:

public Command<MyItem> LabelTappedCommand { get; set; }

The idea is to create a single Command object which gets shared between each cell by also setting the CommandParameter property, but I'm stuck with the Command property right now.

Is this possible?

user246392
  • 2,661
  • 11
  • 54
  • 96
  • Have a look at control template binidng: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/templates/control-templates/template-binding – Ben Reierson Mar 14 '19 at 00:00

2 Answers2

1

This is certainly possible with 2 easy steps.

  1. Give your xaml page an x:Name=MainPage.
  2. Reference it in the binding like this Command="{Binding Path=BindingContext.MyProperty, Source={x:Reference MainPage}}"

This is under the assumption that the BindingContext of your main page is set to your MainPageViewModel but either way you get the idea.

  • 1
    I had this exact solution when `MyControl` did not exist (i.e. I'd had all the XAML dumped into MainPage). I refactored the code into its own custom UI control, but for some reason, the control can't pick up the name `MainPage` when I do `{x:Reference MainPage}`. – user246392 Mar 13 '19 at 16:43
  • I'm getting `Xamarin.Forms.Xaml.XamlParseException: Position 64:26. Can not find the object referenced by MainPage` – user246392 Mar 13 '19 at 16:45
  • My bad, I did not read that right. In that case I'm not sure. You might need to define a property on your custom control for the origional binding context and bind to that in the mainpage xaml? –  Mar 13 '19 at 16:49
  • Did some searching, have you tried something like this: https://stackoverflow.com/questions/15789090/bind-to-a-property-in-the-parent-control ? –  Mar 13 '19 at 17:02
  • Thanks. AFAIK, Forms does not have RelativeSource binding yet. :( – user246392 Mar 13 '19 at 17:14
  • There is even a PR for this, but looks like it hasn't gone anywhere: https://github.com/xamarin/Xamarin.Forms/pull/4375 – user246392 Mar 13 '19 at 17:39
  • Shame, then I think your left with either adding it as a bindable property to your custom control or try some codebehind tricks in that control to get the bindingcontext of it's parent element. But both don't feel like a completely clean solution. –  Mar 13 '19 at 17:41
0

I think that the only solution is to expose Command binding through the MyControl.cs. After that you can use the solution that references some parent control as the binding context (described in answer by @Knoop and you said you are aware how it works so I am not going into details regarding this).

Ivan Ičin
  • 9,672
  • 5
  • 36
  • 57
  • I tried to do that but had difficulty getting the command invoked. I'll give it more attention and see what I can come up with. – user246392 Mar 13 '19 at 17:45