0
votes

I'm building a Xamarin Forms application and am trying to reference the binding context of a parent control from a DataTemplate:

    <flv:FlowListView Grid.Row="3" x:Name="abc"
                Grid.Column="0"
                Grid.ColumnSpan="2"
                FlowColumnCount="3"
                SeparatorVisibility="None"
                HasUnevenRows="true"
                FlowItemsSource="{Binding ProductPackageBatch}"
                FlowLastTappedItem="{Binding LastTappedItem}"
                FlowItemTappedCommand="{Binding ItemTappedCommand}">
                <flv:FlowListView.FlowColumnTemplate>
                    <DataTemplate>
                        <StackLayout Margin="5, 10, 10, 5">
                            <StackLayout.Children>
                                <Frame Padding="2"
                                       OutlineColor="{DynamicResource ProductGridImageBorderColor}"
                                       HasShadow="False"
                                       BackgroundColor="{DynamicResource ProductGridBackgroundColor}">
                                    <Image Aspect="AspectFit"
                                           WidthRequest="100"
                                           Source="{Binding ImageBase64, Converter={StaticResource Base64ToImageConverter}}">
                                        <Image.HeightRequest>
                                            <OnPlatform x:TypeArguments="x:Double"
                                            iOS="100"
                                            Android="150" />
                                        </Image.HeightRequest>
                                    </Image>
                                </Frame>

                                <Label Style="{StaticResource ProductGridName}"
                                    Text="{Binding Name}" />

                                <Label Style="{StaticResource ProductGridFormattedPrice}"
                                    Text="{Binding FormattedPrice}"
                                    FontAttributes="Bold" />

                                <Button BackgroundColor="{DynamicResource BuyButtonColor}"
                                    TextColor="{DynamicResource TextColor}"
                                    BorderRadius="0"
                                    Margin="5, 0, 5, 0"
                                    VerticalOptions="End"
                                    Text="Buy"
                                    Command="{Binding Source={x:Reference abc}, Path=BindingContext.AddToUserCartCommand}"
                                    CommandParameter="{Binding .}" />
                            </StackLayout.Children>
                        </StackLayout>
                    </DataTemplate>
                </flv:FlowListView.FlowColumnTemplate>
            </flv:FlowListView>

There is an event called on AddToUserCartCommand that I want to call from a button in the FlowListView. I've tried a few different ways to reference the control, but the method never gets called. The following button sits outside this control and works ok:

        <Button Grid.Row="4"
                        Margin="30"
                        BackgroundColor="{DynamicResource BuyButtonColor}"
                        TextColor="{DynamicResource TextColor}"
                        BorderRadius="0"
                        Text="Add to Cart"
                        Command="{Binding AddToUserCartCommand}"
                        CommandParameter="{Binding ProductPackage.Id}" />

Can anyone help explain what I'm doing wrong?

1
Thanks for your comment - I hadn't read that article before, however the it did lead me to the solution - turns out that as well as referencing the page instead of the ListView, I was also passing the wrong type as the Command Parameter.markpirvine

1 Answers

0
votes

Set the correct binding context of the Command and CommandParamter:

   <Button BackgroundColor="{DynamicResource BuyButtonColor}"
                                        BindingContext="{x:Reference Name=abc}"
                                        TextColor="{DynamicResource TextColor}"
                                        BorderRadius="0"
                                        Margin="5, 0, 5, 0"
                                        VerticalOptions="End"
                                        Text="Buy"
                                        Command="{Binding Path=BindingContext.AddToUserCartCommand}"
                                        CommandParameter="{Binding Path=BindingContext.ProductPackage.Id}" />