【问题标题】:Send listbox selected items to another listbox on a different uwp page将列表框选定的项目发送到不同 uwp 页面上的另一个列表框
【发布时间】:2019-05-10 05:38:30
【问题描述】:

我在第一页有一个汽车物品清单。我想单击列表中的汽车,然后单击一个按钮,该按钮会将所选汽车发送到另一个 uwp 页面上的列表。
我试图将listCar.selecteditems 放在我创建的名为purchase 的空列表中,并在第2 页显示purchase。使用我目前拥有的代码,它仅在第2 页的列表中显示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>

【问题讨论】:

    标签: c# xaml uwp listbox listboxitem


    【解决方案1】:

    这会将您选择的汽车列表对象作为参数及其所有属性传输到下一页。

    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; }
        }    
    

    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);
            }
        }
    

    【讨论】:

    • 我已经尝试过这个解决方案,但在我的选择列表中它仍然只显示我的解决方案的名称_汽车而不是列表中的对象。它应该显示图片、汽车名称和汽车价格
    • 为此您需要更改列表视图的项目模板。如果你需要也可以添加。
    • 如果可以的话,我将不胜感激,我已经编辑了上面的代码以显示 listbox.itemtemplate
    • 我已经添加了完整的工作示例并编辑了我的答案
    猜你喜欢
    • 2019-05-09
    • 2014-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多