C# 将listbox所选项目发送到其他uwp页面上的另一个listbox

C# 将listbox所选项目发送到其他uwp页面上的另一个listbox,c#,xaml,uwp,listbox,listboxitem,C#,Xaml,Uwp,Listbox,Listboxitem,我在第一页有一个关于汽车物体的清单。我想从列表中单击一辆车,然后单击一个按钮,该按钮会将选定的车发送到另一个uwp页面上的列表中。 我正在尝试将列表放入汽车。在我创建的名为purchase的空列表中选择editems,并在第2页显示purchase。根据我目前掌握的代码,它仅在第2页的列表中显示ProjectName\u Car。 任何关于正确显示此信息的帮助都将不胜感激 Page1.xaml.cs private void Add_Click(object sender, RoutedEven

我在第一页有一个关于汽车物体的清单。我想从列表中单击一辆车,然后单击一个按钮,该按钮会将选定的车发送到另一个uwp页面上的列表中。
我正在尝试将
列表放入汽车。在我创建的名为
purchase
的空列表中选择editems
,并在第2页显示
purchase
。根据我目前掌握的代码,它仅在第2页的列表中显示
ProjectName\u Car

任何关于正确显示此信息的帮助都将不胜感激

Page1.xaml.cs

private void Add_Click(object sender, RoutedEventArgs e)
    {
        var query = listCars.SelectedItems.ToList().Cast<Car>();

        foreach (var item in query)
        {
            purchase.Add(item);
        }

        liistCar.ItemsSource = purchase;
        Frame.Navigate(typeof(Page2), listCar.Items);
    }
private void Add\u单击(对象发送者,路由目标)
{
var query=listCars.SelectedItems.ToList().Cast();
foreach(查询中的var项)
{
购买。添加(项目);
}
liistCar.ItemsSource=采购;
框架.导航(类型(第2页),列表车.项目);
}
Page2.xaml.cs

protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        lstCars.ItemsSource =  e.Parameter as List<Car> ;

    }
  public sealed partial class Page2 : Page
    {
        public Page2()
        {
            this.InitializeComponent();
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            var items  =   e.Parameter as List<Car>;
            listCars.ItemsSource = items;
            base.OnNavigatedTo(e);
        }
    }
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
基地。导航到(e);
lstCars.ItemsSource=e.参数列表;
}
编辑:Page1.xaml

 <ListBox Name="listCars" ItemsSource="{x:Bind cars}" >
            <ListBox.ItemTemplate>
                <DataTemplate x:DataType="Car">
                    <StackPanel Padding="20">
                        <Image Width="200" Height="150" HorizontalAlignment="Left" Source="{x:Bind imgCar}" />
                        <TextBlock FontSize="22" HorizontalAlignment="Left" Text="{x:Bind Name}" Style="{StaticResource HeaderTextBlockStyle}"/>
                        <TextBlock FontSize="16" HorizontalAlignment="Left"> € <Run Text="{Binding Price}" /></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

此选项将选定的汽车列表对象及其所有属性作为参数传输到下一页

MainPage.xaml

<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    x:Name="YourPage"

    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel  x:Name="Stacky" HorizontalAlignment="Stretch" Background="Aqua" VerticalAlignment="Stretch">
        <ListBox Name="listCars" ItemsSource="{x:Bind cars}" SelectionMode="Multiple"  Grid.Column="0"  Grid.Row="1" Background="#FFF1EFEF" Opacity="0.7" Foreground="Black" Margin="10,10,94,10" Grid.RowSpan="2">
            <ListBox.ItemTemplate>
                <DataTemplate x:DataType="local:Car">
                    <StackPanel Padding="20" BorderThickness="2" BorderBrush="Black">
                        <Image Width="200" Height="150" HorizontalAlignment="Left" Source="{x:Bind imgCar}" />
                        <TextBlock FontSize="22" HorizontalAlignment="Left" Text="{x:Bind name}" Style="{StaticResource HeaderTextBlockStyle}"/>
                        <TextBlock FontSize="16" HorizontalAlignment="Left"> € <Run Text="{x:Bind price}" /></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Button Content="Done Selecting" Click="Add_Click"></Button>
    </StackPanel>
</Page>
MainPage.xaml.cs

public sealed partial class MainPage : Page
    {

        List<Car> cars = new List<Car>();
        public MainPage()
        {
            cars.Add(new Car() { imgCar = "ms-appx:///Assets/1.jpg", name = "Car1", price = "10000" });
            cars.Add(new Car() { imgCar = "ms-appx:///Assets/2.jpg", name = "Car2", price = "10001" });
            cars.Add(new Car() { imgCar = "ms-appx:///Assets/3.jpg", name = "Car3", price = "10002" });
            this.InitializeComponent();    

        }
        private void Add_Click(object sender, RoutedEventArgs e)
        {
            List<Car> mySelectedItems = new List<Car>();
            foreach (Car item in listCars.SelectedItems)
            {
                mySelectedItems.Add(item);
            }
            Frame.Navigate(typeof(Page2), mySelectedItems);
        }
    }
    public class Car
    {
        public string imgCar { get; set; }
        public string name { get; set; }
        public string price { get; set; }
    }    
公共密封部分类主页面:第页
{
列出车辆=新列表();
公共主页()
{
cars.Add(新车{imgCar=“ms-appx:///Assets/1.jpg,name=“Car1”,price=“10000”});
cars.Add(新车{imgCar=“ms-appx:///Assets/2.jpg,name=“Car2”,price=“10001”});
cars.Add(新车{imgCar=“ms-appx:///Assets/3.jpg,name=“Car3”,price=“10002”});
this.InitializeComponent();
}
私有无效添加\单击(对象发送者,路由目标)
{
List mySelectedItems=新列表();
foreach(列表CARS中的Car项目。选择EditEMS)
{
mySelectedItems.Add(项目);
}
Frame.Navigate(typeof(第2页),mySelectedItems);
}
}
公车
{
公共字符串imgCar{get;set;}
公共字符串名称{get;set;}
公共字符串price{get;set;}
}    
第2.xaml页

<Page
    x:Class="App1.Page2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <ListBox Name="listCars"  SelectionMode="Multiple"  Grid.Column="0"  Grid.Row="1" Background="#FFF1EFEF" Opacity="0.7" Foreground="Black" Margin="10,10,94,10" Grid.RowSpan="2">
            <ListBox.ItemTemplate>
                <DataTemplate x:DataType="local:Car">
                    <StackPanel Padding="20" BorderThickness="2" BorderBrush="Black">
                        <Image Width="200" Height="150" HorizontalAlignment="Left" Source="{x:Bind imgCar}" />
                        <TextBlock FontSize="22" HorizontalAlignment="Left" Text="{x:Bind name}" Style="{StaticResource HeaderTextBlockStyle}"/>
                        <TextBlock FontSize="16" HorizontalAlignment="Left"> € <Run Text="{x:Bind price}" /></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Page>
Page2.xaml.cs

protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        lstCars.ItemsSource =  e.Parameter as List<Car> ;

    }
  public sealed partial class Page2 : Page
    {
        public Page2()
        {
            this.InitializeComponent();
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            var items  =   e.Parameter as List<Car>;
            listCars.ItemsSource = items;
            base.OnNavigatedTo(e);
        }
    }
公共密封部分类第2页:第
{
公共页2()
{
this.InitializeComponent();
}
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
var项目=e.参数为列表;
listCars.ItemsSource=项目;
基地。导航到(e);
}
}

我尝试过此解决方案,但在我选择的列表中,它仍然只显示我的解决方案车的名称,而不显示列表中的对象。它应该显示一个图像、汽车名称和汽车价格,您需要更改listview的itemtemplate。如果你需要,我也会添加。如果你可以的话,我会非常感激,我已经编辑了上面的代码以显示listbox.ItemTemplate我已经添加了完整的工作示例并编辑了我的答案