如何在列表中获取SelectedItem的价值



我想在listView中获取SelectedItem的值,这是我的代码。

public class BlogClass{
     public string NewId;
     public BlogClass()
     {
      additem();
     }

    public class BlogViews
    {
        public string id { get; set; }
        public string DisplayTopic { get; set; }
        public string DisplayMain { get; set; }
        public ImageSource BlogImageSource { get; set; }
    }
    public List<BlogViews> BlogList1 = new List<BlogViews>();
    public void additem()
    {
        BlogList1.Add(new BlogViews { id = "1", DisplayMain = "Margret", DisplayTopic = "Mensah" });
        BlogList1.Add(new BlogViews { id = "2", DisplayMain = "Maet", DisplayTopic = "Meah" });
        BlogList1.Add(new BlogViews { id = "3", DisplayMain = "dargret", DisplayTopic = "sah" });
        BlogList1.Add(new BlogViews { id = "4", DisplayMain = "gret", DisplayTopic = "Meh" });
        BlogListView.ItemsSource = BlogList1;
    }

}

    <?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="demoListView.ImageCellPage">
    <ContentPage.Content>
        <ListView  x:Name="BloglistView">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout BackgroundColor="#eee"
                        Orientation="Vertical">
                            <StackLayout Orientation="Horizontal">
                                <Image Source="{Binding   BlogImageSource}" />
                                <Label Text="{Binding id}"
                                TextColor="#f35e20" />
                                <Label Text="{Binding DisplayTopic}"
                                HorizontalOptions="EndAndExpand"
                                TextColor="#503026" />
                               <Label Text="{Binding DisplayMain}"
                                HorizontalOptions="EndAndExpand"
                                TextColor="#503026" />
                            </StackLayout>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage.Content>
</ContentPage>

问题

现在,当我在bloglistview上选择一个项目时,我想根据SelectedItem将newID的值设置为ID的值。

您只需要为listView添加ItemTapped事件:

XAML:

<ListView  x:Name="BloglistView" ItemTapped="Handle_ItemTapped">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout BackgroundColor="#eee"
                        Orientation="Vertical">
                    <StackLayout Orientation="Horizontal">
                        <Image Source="{Binding   BlogImageSource}" />
                        <Label Text="{Binding id}"
                                TextColor="#f35e20" />
                        <Label Text="{Binding DisplayTopic}"
                                HorizontalOptions="EndAndExpand"
                                TextColor="#503026" />
                        <Label Text="{Binding DisplayMain}"
                                HorizontalOptions="EndAndExpand"
                                TextColor="#503026" />
                    </StackLayout>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

代码背后:

void Handle_ItemTapped(object sender, Xamarin.Forms.ItemTappedEventArgs e)
{
   var selectedItem = e.Item as BlogViews;   
   NewId = selectedItem.id;
}

最新更新