Кроме ответа Джейсона, я предлагаю вам также получить любой текст метки с помощью 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; }
}