I am using bindable stack layout with ItemTemplateSelector. My DataTemplates are in another file which is ResourceDictionary included in MainView as MergedResourceDictionay. In one of my DataTempplates I have Label with TapGestureRecognizer that is supposed to trigger command in MainViewViewModel, and that I can't seem to get working....
I tried having Source={x:Reference MainPage}
in my Command binding, but can't reference it since it's not in same file
(Xamarin.Forms.Xaml.XamlParseException: 'Position 28:51. Can not find the object referenced by MainPage')
<--! this is snippet from MainPage -->
<ScrollView Orientation="Vertical" Grid.Row="1">
<local:BindableStackLayout BindableLayout.ItemsSource="{Binding Day.TodayEntry}"
x:Name="BindableStack" Spacing="10" Margin="10"
BindableLayout.ItemTemplateSelector="{StaticResource CardDetailTemplateSelector}"/>
</ScrollView>
<--! this is problematic snippet from data template -->
<Label Text="REMOVE" FontSize="Medium" TextColor="White" HorizontalOptions="End" Margin="3,0,0,0">
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding RemoveEntryCommand, Source={x:Reference MainPage}}"
CommandParameter="{Binding .}"/>
</Label.GestureRecognizers>
</Label>
The reason that does not work is that XAML compilation look-ups are page-specific which means if you have two different pages and you search using names it will not work in most cases.
What I usually do in such scenarios is I use the parent keyword for my BindingContext!
So assuming your Label (which is in the ViewCell) has a parent which is a Grid you can do something like
Command="{Binding Source={x:Reference parentLayoutGrid}, Path=Parent.Parent.BindingContext.RemoveEntryCommand}"
Where the number of parents properties required depends on your ViewHeirarchy and which View would have the correct context.
Good luck.
Feel free to get back in case of queries
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.