[英]how to select an item in listbox in windows phone 7.1
我是初學者並為Windows Phone 7.1創建應用程序,我試圖通過列表框中的單擊事件導航到新頁面,基本上我希望列表框中的每個項目都應該導航到不同的頁面我嘗試過以下代碼
private void FirstListBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
// If selected index is -1 (no selection) do nothing
if (FirstListBox.SelectedIndex == -1)
return;
// Navigate to the new page
NavigationService.Navigate(new Uri("/FirstItemPage1.xaml?selectedItem=" + FirstListBox.SelectedIndex, UriKind.Relative));
// Reset selected index to -1 (no selection)
FirstListBox.SelectedIndex = -1;
}
上面的代碼運作良好,但問題是它需要整個列表框到同一頁面,但我希望每個單獨的項目導航到不同的頁面
將ListBox中的每個項目添加到ListBox時,可以為其設置SelectedValue。
假設您通過代碼將List字符串列表分配給ListBox的ItemsSource(或者它可能通過數據綁定發生)。 您可以在頁面的構造函數中添加項目。
var items = new List<string>(){ "Home", "Details" };
FirstListBox.ItemsSource = items;
在SelectionChanged事件中添加一個switch語句,該語句根據所選項的SelectedValue導航到不同的頁面,如下所示:
private void FirstListBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
// If selected index is -1 (no selection) do nothing
if (FirstListBox.SelectedIndex == -1)
return;
switch (FirstListBox.SelectedValue)
{
case "Home":// Navigate to the page
NavigationService.Navigate(new Uri("/Home.xaml", UriKind.Relative)); break;
case "Details":// Navigate to the page
NavigationService.Navigate(new Uri("/Details.xaml", UriKind.Relative)); break;default:break;
}
// Reset selected index to -1 (no selection)
FirstListBox.SelectedIndex = -1;
}
如果將復雜對象綁定到ListBox,則可以使用ListBox的SelectedItem-Property,因為該屬性包含已綁定到該ListBoxItem的對象,並根據復雜對象的某個屬性區分這些情況,如姓名或身份證。
我讓我的應用程序使用此代碼
private void SecondListBox_SelectionChanged(object sender,SelectionChangedEventArgs e){{if(SecondListBox.SelectedIndex == -1)return;
ItemViewModel itemViewModel = SecondListBox.SelectedItem as ItemViewModel ;
switch (itemViewModel.LineOne)
{
case "Violet":
NavigationService.Navigate(new Uri("/SecondListPage1.xaml?selectedItem=" + SecondListBox.SelectedIndex, UriKind.Relative));
break;
case "Indigo":
NavigationService.Navigate(new Uri("/SecondListPage2.xaml?selectedItem=" + SecondListBox.SelectedIndex, UriKind.Relative));
break;
case "Blue":
NavigationService.Navigate(new Uri("/SecondListPage3.xaml?selectedItem=" + SecondListBox.SelectedIndex, UriKind.Relative));
break;
case "Green":
NavigationService.Navigate(new Uri("/SecondListPage4.xaml?selectedItem=" + SecondListBox.SelectedIndex, UriKind.Relative));
break;
case "Yellow":
NavigationService.Navigate(new Uri("/SecondListPage5.xaml?selectedItem=" + SecondListBox.SelectedIndex, UriKind.Relative));
break;
case "Orange":
NavigationService.Navigate(new Uri("/SecondListPage6.xaml?selectedItem=" + SecondListBox.SelectedIndex, UriKind.Relative));
break;
case "Red":
NavigationService.Navigate(new Uri("/SecondListPage7.xaml?selectedItem=" + SecondListBox.SelectedIndex, UriKind.Relative));
break;
case "Purple":
NavigationService.Navigate(new Uri("/SecondListPage8.xaml?selectedItem=" + SecondListBox.SelectedIndex, UriKind.Relative));
break;
default:
MessageBox.Show("Please Select From the list!");
break;
}
SecondListBox.SelectedIndex = -1;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.