【问题标题】:Xamarin Forms Binding to Parent ControlXamarin 表单绑定到父控件
【发布时间】:2017-07-15 15:06:45
【问题描述】:

我正在构建一个 Xamarin Forms 应用程序并尝试从 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>

有一个调用 AddToUserCartCommand 的事件,我想从 FlowListView 中的按钮调用该事件。我尝试了几种不同的方法来引用控件,但该方法永远不会被调用。以下按钮位于此控件之外并且可以正常工作:

        <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}" />

谁能帮忙解释一下我做错了什么?

【问题讨论】:

标签: binding xamarin.forms


【解决方案1】:

设置Command和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}" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-29
    • 2018-06-27
    • 2018-11-20
    • 1970-01-01
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多