使用ListBox中的SelectedIndex显示有关ItemPage的信息

时间:2013-06-26 00:47:52

标签: c# windows-phone-8 azure-mobile-services

问题

我正在尝试显示有关从ListBox中选择的特定项目的信息。这些项目来自Azure移动服务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));
    }

2 个答案:

答案 0 :(得分:0)

获取SelectedIndex而不是捕获SelectedItem,而FirstName应该是具有属性LastNamevar 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(); 的对象(可能是您定义的某个类)。从那里,您可以使用这些值查询数据库。类似于下面代码的东西:

{{1}}

答案 1 :(得分:0)

我会把它改成更像这个

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

的OnNavigatedTo

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