首页 > 解决方案 > 我如何控制列表视图项目的详细信息?

问题描述

我有 2 页有自己的列表视图。我需要将第一个列表视图项目的所有详细信息添加到另一个列表视图中。感谢您对我模糊的问题的任何帮助。

public async void OnTapped(object sender, ItemTappedEventArgs e)
{
    await Navigation.PushAsync(new TeamPage());
}

因此,当一个项目被点击时,它会切换到另一个页面,但希望将所选项目添加到 TeamPage 列表视图中。项目的所有细节都绑定到一个 IList。

标签: c#listviewxamarin.forms

解决方案


根据您的描述,您想点击 ListView 项目,然后导航到另一个页面,在此页面列表视图中显示点击的项目,对吗?如果是,我会做一个样本,你可以看看:

第 1 页:

   <StackLayout>
        <ListView ItemsSource="{Binding model3s}" ItemTapped="ListView_ItemTapped">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal">
                            <Label Text="{Binding Id}" />
                            <Label Text="{Binding FirstName}" />
                            <Label Text="{Binding LastName}" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>

public partial class Page11 : ContentPage
{

    public ObservableCollection<model3> model3s { get; set; }
    public Page11 ()
    {
        InitializeComponent ();
        model3s = new ObservableCollection<model3>()
        {
            new model3(){Id=1,FirstName="Cherry",LastName="Bu",Description="Age is 28, class is A"},
            new model3(){Id=2,FirstName="Barry",LastName="Bu",Description="Age is 32, class is B"},
            new model3(){Id=3,FirstName="Wendy",LastName="Bu",Description="Age is 25, class is C"},
            new model3(){Id=4,FirstName="Stnfan",LastName="Bu",Description="Age is 27, class is A"},
            new model3(){Id=5,FirstName="Annie",LastName="Bu",Description="Age is 28, class is B"},
            new model3(){Id=6,FirstName="Mattew",LastName="Bu",Description="Age is 25, class is C"},
            new model3(){Id=7,FirstName="Amy",LastName="Bu",Description="Age is 29, class is A"},
            new model3(){Id=8,FirstName="Elvis",LastName="Bu",Description="Age is 32, class is B"},
        };
        this.BindingContext = this;
    }

    private async void ListView_ItemTapped(object sender, ItemTappedEventArgs e)
    {
        var content = e.Item as model3;
        await Navigation.PushModalAsync(new Page12(content));
    }
}

public class model3
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public string Description { get; set; }
}

第2页:

<StackLayout>
        <ListView x:Name="listview1" ItemsSource="{Binding collection}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal">
                            <Label Text="{Binding FirstName}" />
                            <Label Text="{Binding LastName}" />
                            <Label Text="{Binding Description}" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>

public partial class Page12 : ContentPage
{
   public ObservableCollection<model3> collection { get; set; }

    public Page12 (model3 content)           
    {

        InitializeComponent ();
        collection = new ObservableCollection<model3>();
        collection.Add(content);


        this.BindingContext = this;
    }
}

推荐阅读