c# - 将列表框选定项目发送到不同 uwp 页面上的另一个列表框
问题描述
我在第一页有一个汽车物品清单。我想从列表中单击一辆车,然后单击一个按钮,该按钮会将选定的汽车发送到另一个 uwp 页面上的列表中。
我正在尝试将其放入我创建的名为并显示在第 2 页listCar.selecteditems
的空列表中。使用我目前拥有的代码,它仅显示在第 2 页的列表中。
感谢任何有关正确显示此内容的帮助。purchase
purchase
ProjectName_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);
}
Page2.xaml.cs
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
lstCars.ItemsSource = e.Parameter as List<Car> ;
}
编辑: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>
解决方案
这会将您选择的汽车列表对象作为参数及其所有属性传输到下一页。
主页.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; }
}
Page2.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
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);
}
}
推荐阅读
- python - 使用参数运行 python 脚本
- javascript - 运行 jquery 一旦 div 的内容已完全加载
- python - 如何在我的 OpenCV 绘画应用程序中修复此错误?
- sql - Oracle SQL 查询删除逗号前的空格
- python - 如何在 conv2d 层中使用 PReLU
- r - 在R中用最常见的值替换值
- docker - 码头集装箱 ID 未显示
- typescript - 如何为导出函数的模块创建声明文件?
- api - 得到那个错误“类型'String'不是'index'类型'int'的子类型”颤动
- python - 即使在 python 中调整大小后,如何从静态图像中获取特定的极值点数?