问题
我正在尝试显示有关从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 页面,如上所示。然后用于查询数据库。这个对吗?MainListBox
SelectedItem
编辑 2
private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
NavigationService.Navigate(new Uri("/ViewModels/Phones/PhoneItemView.xaml?selectedItem=" + MainListBox.SelectedItem, UriKind.Relative));
}