Xamarin-从collectionView单击的单元格内部的标签中获取值

问题描述 投票:0回答:1

我有一个collectionView,它是通过Binding源自动填充的。在每个单元格中,我都有一些标签。单击标签时,我需要能够读取其中一个标签的值。(在下面的示例中为“ PhotographerCode”)这是到目前为止的内容,但是我无法弄清楚事件触发后即可从代码访问标签...

<StackLayout>
        <Button Text="Populate DB" 
                x:Name="populateButton" 
                Clicked="populateButton_Clicked"/>
        <CollectionView ItemsSource="{Binding GalleryList}">
            <CollectionView.ItemsLayout>
                <GridItemsLayout Orientation="Vertical"
                    Span="1" />
            </CollectionView.ItemsLayout>
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <ViewCell x:Name="Cell"  Tapped="Cell_Tapped">
                    <Grid Padding="10" ColumnSpacing="0" RowSpacing="0">


                        <Label Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" Text="{Binding GalleryName}" FontSize="Large" BackgroundColor="LightGray"></Label>
                        <Label Grid.Row="1" Grid.Column="0" Text="Photographer: " FontSize="Small"  BackgroundColor="LightGray"></Label>
                        <Label Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" Text="{Binding PhotographerName}" FontSize="Default" BackgroundColor="LightGray"></Label>
                        <Label Grid.Row="2" Grid.Column="0" Text="Code: " FontSize="Small" BackgroundColor="LightGray"></Label>
                        <Label x:Name="PhotographerCode" Grid.Row="2" Grid.Column="1" Text="{Binding PhotographerCode}" FontSize="Small" BackgroundColor="LightGray"></Label>
                        <Label Grid.Row="2" Grid.Column="2" Text="{Binding GalleryCode}" FontSize="Small" BackgroundColor="LightGray"></Label>
                    </Grid>
                    </ViewCell>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </StackLayout>

和.cs文件:

 private void Cell_Tapped(object sender, EventArgs e)
    {
       ....
    }
xamarin label cell collectionview
1个回答
0
投票

[CollectionView已内置对此的支持

<CollectionView ItemsSource="{Binding GalleryList}" SelectionMode="Single" SelectionChanged="OnCollectionViewSelectionChanged">

然后在事件处理程序中

void OnCollectionViewSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var item = (MyModelClass)e.CurrentSelection.FirstOrDefault();

    ....
}

您也不需要(也不应该)在CollectionView中使用任何* Cell元素。这些是ListView所必需的,并已由具有CollectionView的通用模板替换]

© www.soinside.com 2019 - 2024. All rights reserved.