1

问题

我正在尝试显示有关从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>

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

更新 - 来自 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上,然后将其传递到 Item.xaml 页面,如上所示。然后用于查询数据库。这个对吗?MainListBoxSelectedItem

编辑 2

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

2 回答 2

0

而不是捕获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();
于 2013-06-26T02:20:35.220 回答
0

我会把它改成这样

应用程序.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;
}
于 2013-07-02T13:46:27.397 回答