1

問題

から選択した特定のアイテムに関する情報を表示しようとしていますListBox。項目は、Azure Mobile Services SQL データベースからのものです。をキャプチャしてから、 が に等しいSelectedIndexデータベースにクエリを実行しようとしました。残念ながら、ユーザーにはデータを並べ替えるオプションが与えられているため、アイテムの ID をデータベースからのアイテムと一致しなくなった場所に変更します。RowIDSelectedIndexRowID

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プロパティから項目が選択されたページではMainListBoxSelectedItem上記のように Item.xaml ページに渡されます。これは、データベースのクエリに使用されます。これは正しいです?

編集 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

私はそれをもっとこのように変更します

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;
}
于 2013-07-02T13:46:27.397 に答える