我正在尝试显示有关从ListBox
中选择的特定项目的信息。这些项目来自Azure移动服务SQL数据库。我尝试捕获SelectedIndex
,然后查询RowID
等于SelectedIndex
的数据库。不幸的是,用户可以选择对数据进行排序,从而将项目的ID更改为不再与数据库中的RowID
匹配。
<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
的值,然后根据这些值查询数据库吗?
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页面,如上所示。然后用于查询数据库。这是对的吗?
private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
NavigationService.Navigate(new Uri("/ViewModels/Phones/PhoneItemView.xaml?selectedItem=" + MainListBox.SelectedItem, UriKind.Relative));
}
答案 0 :(得分:0)
获取SelectedIndex
而不是捕获SelectedItem
,而FirstName
应该是具有属性LastName
和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();
的对象(可能是您定义的某个类)。从那里,您可以使用这些值查询数据库。类似于下面代码的东西:
{{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;
}