简体   繁体   中英

How do I get selecteditem from ListView in XAML?

This may be a duplicate. But I have tried everything I could find, and I cannot make it work.

Why does this not work? SelectedCourse is allways null. I have tested everything else, and it works fine. It is just getting the selected Item who is the problem.

This is my ViewModel:

public Course SelectedCourse { get; set; }

public async Task CoursesListViewClick(object sender, ItemClickEventArgs e)
        {
            var students = await coursesDataAccess.GetCourseStudentsAsync(SelectedCourse.CourseID); //THIS IS NULL
            foreach (Student s in students)
                CourseStudents.Add(s);
        } 

This is my XAML code:

           <ListView Grid.Column="0"
                      x:Name="lvCourses"
                      IsItemClickEnabled="True"
                      DataContext="{x:Bind ViewModel}"
                      ItemsSource="{Binding Courses, Mode=OneWay}"
                      SelectedItem="{Binding SelectedCourse, Mode=TwoWay}"
                      ItemClick="{x:Bind ViewModel.CoursesListViewClick}"
                      Margin="10">
                <ListView.ItemTemplate>
                    <DataTemplate x:DataType="model:Course">
                        <StackPanel Height="250">
                            <TextBlock
                                FontSize="50"
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center"
                                Text="{x:Bind Name}"
                                Style="{StaticResource TitleTextBlockStyle}"/>
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

Why does this not work? SelectedCourse is allways null.

Since you have bound the SelectedItem property and ItemClick event, when you click the listViewItem, it will first trigger the CoursesListViewClick method which bound with the ItemClick event and then trigger the setter method of SelectedCourse to set the value. So in the CoursesListViewClick method, the SelectedCourse hasn't been assigned a value, it is always null.

The e.ClickedItem from CoursesListViewClick method represents the current item you clicked, you can directly use it to operate.

public async Task CoursesListViewClick(object sender, ItemClickEventArgs e)
{
    Course selectedCourse = e.ClickedItem as Course;
    var students = await coursesDataAccess.GetCourseStudentsAsync(selectedCourse.CourseID); //THIS IS NULL
    foreach (Student s in students)
        CourseStudents.Add(s);
}

if you use: listView.ItemTapped += async (sender, e) => { e.Item; } listView.ItemTapped += async (sender, e) => { e.Item; } you can then manipulate what you want to do depending on the item id

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM