使用ListBox中的SelectedIndex在ItemPage WITH上显示信息

本文关键字:WITH 显示 信息 ItemPage ListBox 中的 SelectedIndex 使用 | 更新日期: 2023-09-27 18:20:04

问题

我正在尝试显示从ListBox中选择的特定项目的信息。这些项目来自Azure Mobile Services SQL数据库。我尝试捕获SelectedIndex,然后查询数据库,其中RowID等于SelectedIndex。不幸的是,用户可以选择对数据进行排序,因此将项目的ID更改为与数据库中的RowID不再匹配的位置。

Main.xaml中的代码

        <Grid x:Name="ContentPanel" Margin="10,97,12,0" Grid.RowSpan="2">
        <ListBox x:Name="MainListBox" Margin="10,10,-12,0" SelectionChanged="MainListBox_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="0,0,0,17" Width="432">
                        <TextBlock Text="{Binding FirstName}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                        <TextBlock Text="{Binding LastName}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

现在,是否可以以某种方式捕获FirstNameLastName的值,然后根据这些值查询数据库?

更新-Item.xaml中的代码

private MobileServiceCollection<Phones, Phones> items;
    private IMobileServiceTable<Phones> phoneTable =
        App.MobileService.GetTable<Phones>();
    public PhoneItemView()
    {
        InitializeComponent();
    }
    protected async override void OnNavigatedTo(NavigationEventArgs e)
    {
        var listItem = MainListBox.SelectedItem as Phones;                        
        string selectedIndex = "";
        if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
        {
            //Start progressBar
            progressBar1.IsEnabled = true;
            progressBar1.IsIndeterminate = true;
            //Query database
            try
            {
                items = await phoneTable
                   .Where(phone => phone.Model == listItem.Model)
                   .ToCollectionAsync();
            }
            catch (MobileServiceInvalidOperationException)
            {
                MessageBox.Show("Error", "Error", MessageBoxButton.OK);
            }
            MainListBox.ItemsSource = items;
            //End progressBar
            progressBar1.IsEnabled = false;
            progressBar1.Visibility = System.Windows.Visibility.Collapsed;
            progressBar1.IsIndeterminate = false;
        }
    }

Main.xaml页面上,当从MainListBox中选择一个项目时,SelectedItem属性将被传递到item.xaml页面,如上图所示。然后使用它来查询数据库。这是正确的吗?

编辑2

private void MainListBox_SelectionChanged(object sender,  SelectionChangedEventArgs e)
    {
        NavigationService.Navigate(new Uri("/ViewModels/Phones/PhoneItemView.xaml?selectedItem=" + MainListBox.SelectedItem, UriKind.Relative));
    }

使用ListBox中的SelectedIndex在ItemPage WITH上显示信息

我会把它改成更像这个

App.xaml.cs

public static Phones SelectedPhone { get; set; }

选择

private void MainListBox_SelectionChanged(object sender,  SelectionChangedEventArgs e)
{
    App.SelectedPhone = MainListBox.SelectedItem as Phones;
    NavigationService.Navigate(new Uri("/ViewModels/Phones/PhoneItemView.xaml, UriKind.Relative));
}

导航至

protected async override void OnNavigatedTo(NavigationEventArgs e)
{
    var listItem = App.SelectedPhone;
    progressBar1.IsEnabled = true;
    progressBar1.IsIndeterminate = true;
    //Query database
    try
    {
        items = await phoneTable
                .Where(phone => phone.Model == listItem.Model)
                .ToCollectionAsync();
     }
     catch (MobileServiceInvalidOperationException)
     {
         MessageBox.Show("Error", "Error", MessageBoxButton.OK);
     }
     MainListBox.ItemsSource = items;
     //End progressBar
     progressBar1.IsEnabled = false;
     progressBar1.Visibility = System.Windows.Visibility.Collapsed;
     progressBar1.IsIndeterminate = false;
}

不要捕获SelectedIndex,而是获取SelectedItem,它应该是一个具有属性FirstNameLastName的对象(可能是您定义的某个类)。从那里,您可以使用这些值查询数据库。类似于下面的代码:

var listItem = MainListBox.SelectedItem as Person;
var table = MobileService.GetTable<Person>();
var serverItem = (await table
   .Where(p => p.FirstName == listItem.FirstName && p.LastName == listItem.LastName)
   .ToEnumerableAsync())
   .FirstOrDefault();