我如何控制Listview项目的详细信息?

时间:2019-05-26 19:50:42

标签: c# listview xamarin.forms

我有2个页面具有自己的列表视图。选择第一个列表视图项时,我需要将其所有细节添加到另一个列表视图中。感谢您对我含糊的问题的任何帮助。

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

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

1 个答案:

答案 0 :(得分:0)

根据您的描述,您想点击ListView Item,然后导航到另一个页面,在此页面的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;
    }
}