首页 > 解决方案 > 与 listview 绑定的 observableCollection 的 Xamarin 返回索引

问题描述

如何获取绑定到列表的 observableCollection 的索引?

我希望当用户单击 listview 或 collectionView 上的按钮时,该按钮命令参数返回单击的列表单元格的索引。

命令方法:

    private void HT(object id)
    {

        var x = (id as CategoriesGroup);

        int si = ExpandedCategories.IndexOf(x);

        Debug.WriteLine("TAAAAAAAAAAAAAAAAPPPPPPPPPPPPEEEEEEEED");
    }

XAML 代码

        <CollectionView ItemsSource="{Binding ExpandedCategories}" IsGrouped="True">
            <CollectionView.ItemsLayout>
                <GridItemsLayout Orientation="Vertical" Span="1" VerticalItemSpacing="5" HorizontalItemSpacing="5"></GridItemsLayout>
            </CollectionView.ItemsLayout>
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <StackLayout >
                        <Label Text="{Binding NameNative}"></Label>
                    </StackLayout>
                </DataTemplate>
            </CollectionView.ItemTemplate>
            <CollectionView.GroupHeaderTemplate>
                <DataTemplate>
                    <StackLayout BackgroundColor="AliceBlue">
                        <Label Text="{Binding Name}" FontSize="Title"></Label>
                        <Button Image="{Binding StateIcon}"
                                BackgroundColor="Transparent"
                                BorderColor="Transparent"
                                BorderWidth="0"
                                Command="{Binding BindingContext.ExpandCommand ,Source={x:Reference ServiceListPage}}"
                                CommandParameter="{Binding}"/>
                    </StackLayout>
                </DataTemplate>
            </CollectionView.GroupHeaderTemplate>
        </CollectionView>

对于录制的第一项,我得到 0 作为正确的索引,其余的我得到 -1

标签: xamarin

解决方案


推荐阅读