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

Xamarin - Get value from label inside clicked cell of collectionView

我有一个 collectionView,它是通过绑定源自动填充的。 在每个单元格中,我都有一些标签。当我点击一个标签时,我需要能够读取其中一个标签的值。(在下面的示例中它是 "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)
    {
       ....
    }

CollectionView 内置了对此的支持

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

然后在您的事件处理程序中

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

    ....
}

您也不需要(也不应该)将任何 *Cell 元素与 CollectionView 一起使用。这些是 ListView 所必需的,并且已被 CollectionView

的通用模板所取代

除了Jason的回复,我建议你也可以通过CollectionView SelectedItem.

获取任何标签文本

这是我的代码,绑定 SelectedItem,模式为 TwoWay。

<StackLayout>

        <CollectionView
            ItemsSource="{Binding GalleryList}"
            SelectedItem="{Binding photo, Mode=TwoWay}"
            SelectionMode="Single">

            <CollectionView.ItemTemplate>
                <DataTemplate>

                    <Grid Margin="10" Padding="10">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                            <RowDefinition Height="*" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>

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

                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </StackLayout>

public partial class Page1 : ContentPage, INotifyPropertyChanged
{
    public ObservableCollection<photo> GalleryList { get; set; }
    private photo _selecteditem;
    public photo selecteditem
    {
        get { return _selecteditem; }
        set
        {
            _selecteditem = value;
            RaisePropertyChanged("selecteditem");
            Console.WriteLine("the selected GalleryName is {0}", selecteditem.GalleryName);

        }
    }
    public Page1()
    {
        InitializeComponent();

        GalleryList = new ObservableCollection<photo>()
        {
            new photo(){GalleryName="photo 1",PhotographerName="test 1",PhotographerCode="test",GalleryCode="test"},
            new photo(){GalleryName="photo 2",PhotographerName="test 2",PhotographerCode="test",GalleryCode="test"},
            new photo(){GalleryName="photo 3",PhotographerName="test 1",PhotographerCode="test",GalleryCode="test"},
            new photo(){GalleryName="photo 4",PhotographerName="test 2",PhotographerCode="test",GalleryCode="test"}
        };

        this.BindingContext = this;

    }      
    public event PropertyChangedEventHandler PropertyChanged;       
    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }        
}

public class photo
{
    public string GalleryName { get; set; }
    public string PhotographerName { get; set; }
    public string PhotographerCode { get; set; }
    public string GalleryCode { get; set; }

}