动态膨胀时获取 ListView 中的哪个复选框被选中

Get which checkbox inside ListView is checked when inflated dynamically

我有这个列表视图:

            <ListView x:Name="listview_brouch" HasUnevenRows="true" Grid.Row="1">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>

                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="4*"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>

                                <Grid.RowDefinitions>
                                    <RowDefinition Height="auto"/>
                                </Grid.RowDefinitions>
                                
                                <Label 
                                    Grid.Column="0"
                                    Padding="0,10,0,10"
                                    FontSize="16"
                                    HorizontalOptions="Start"
                                    VerticalOptions="Start"
                                    FontFamily="ButtonFont"
                                    Text="{Binding title}"
                                    TextColor="Black"
                                    />

                                <CheckBox
                                     CheckedChanged="CheckBox_CheckedChanged"
                                   VerticalOptions="Center"  
                                    HorizontalOptions="Center"
                                    Grid.Column="1"
                                    Color="Aqua"/>

                            </Grid>
                            

                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

它由一个标签和一个复选框组成。我像这样提供列表视图数据:

    private void FillLists()
    {
        List<SubCategories> brouch = new List<SubCategories>();

        foreach (var e in subCategories)
        {
            if(e.IDfromCategories == categories[0].mainCategoryID)
                brouch.Add(e);
        }

        listview_brouch.ItemsSource = brouch;

    }

现在列表会根据 brouch

中的项目数量而膨胀

我现在也会根据 brouch

中的项目来增加复选框的数量

现在我可以像在复选框中那样添加事件处理程序了。

但是,每个复选框都是一样的。我需要知道相应对象的哪个项目 brouch 复选框已被勾选。

假设 brouch

中有三项

项目名称 1 项目名称 2 ITENNAME 3

当我选中复选框 3 时,我需要知道这是来自列表视图中还包含 ITEMNAME 3 的行。

我该如何实现?

谢谢!

使用 BindingContextsender

protected void CheckBox_CheckedChanged(object sender, EventArgs args)
{
  var check = (Checkbox)sender;
  var item = (SubCategories)check.BindingContext;

  // then item contains the object for the given row
}