【问题标题】:How to retrieve objects property info from data binding如何从数据绑定中检索对象属性信息
【发布时间】:2016-06-12 20:22:54
【问题描述】:

我有一个 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");
    }

【问题讨论】:

  • (((ListView)sender).SelectedItem as Student) 对你不起作用?
  • 就是这样!谢谢!

标签: c# wpf xaml xamarin xamarin.forms


【解决方案1】:

感谢@RoyiMindel 指出的非常简单的解决方案,而不是

 DisplayAlert("Item Selected", ((ListView)sender).SelectedItem.ToString(), "OK");

应该是:

DisplayAlert("Item Selected", (((ListView)sender).SelectedItem as Student).complete_name, "OK");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    • 1970-01-01
    • 2011-01-13
    • 1970-01-01
    • 2016-11-21
    • 2020-04-07
    • 2011-01-13
    相关资源
    最近更新 更多