如何获取列表中SelectedItem的值

时间:2019-02-27 10:50:10

标签: c# xamarin

我想在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的值,因此将id的值设置为

1 个答案:

答案 0 :(得分:1)

您只需要为列表视图添加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;
}