我有一个Xamarin Forms应用程序,我在其中填充列表视图,其中列出了Student
类型的对象。我希望能够选择此学生并打开一个显示所选学生姓名的提醒。我当前的尝试仅显示警报中对象的类型(Student
)。这是我的方法:
AttendancePage.xaml:
<ListView x:Name="RosterInView" SeparatorVisibility="None" ItemSelected="OnSelection">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding complete_name}"
Detail="{Binding grade}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
在我的AttendancePage.xaml.cs
中,我很难获得绑定属性:
protected override void OnAppearing()
{
base.OnAppearing();
IEnumerable<Student> roster = _database.GetItems();
RosterInView.ItemsSource = roster;
}
void OnSelection(object sender, SelectedItemChangedEventArgs e)
{
if (e.SelectedItem == null)
return;
// real trouble starts here with how to refer to "complete_name" as seen in the xaml file
DisplayAlert("Item Selected", ((ListView)sender).SelectedItem.ToString(), "OK");
}
答案 0 :(得分:3)
非常简单的解决方案,感谢@RoyiMindel指出,而不是
DisplayAlert("Item Selected", ((ListView)sender).SelectedItem.ToString(), "OK");
它应该是:
DisplayAlert("Item Selected", (((ListView)sender).SelectedItem as Student).complete_name, "OK");