首页 > 解决方案 > 绑定到嵌套项

问题描述

我有这个代码:

<StackLayout BindableLayout.ItemsSource="{Binding Classes}">
    ....
    <StackLayout BindableLayout.ItemsSource="{Binding Class1}">
        ...
        <StackLayout.GestureRecognizers>
            <TapGestureRecognizer Tapped="Class_Tapped" CommandParameter="{Binding .}" />
        </StackLayout.GestureRecognizers>
    </StackLayout>
</StackLayout>

问题是我得到的是 Classes 而不是 Class1 的绑定。我想要 Tapped CommandParameter 中的 Class1 绑定。这可能吗?

谢谢!

标签: xamarin.forms

解决方案


放入 的inner StackLayoutDataTemplateouter StackLayoutBindingContextTapGestureRecognizer 必须是Class1

   <StackLayout BindableLayout.ItemsSource="{Binding Classes}">
        <BindableLayout.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout BindableLayout.ItemsSource="{Binding Class1}">
                        <StackLayout.GestureRecognizers>
                            <TapGestureRecognizer Tapped="Class_Tapped" CommandParameter="{Binding .}" />
                        </StackLayout.GestureRecognizers>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </BindableLayout.ItemTemplate>
    </StackLayout>

推荐阅读