private void my_listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
FrameworkElement element = (FrameworkElement)sender;
SampleData item = (SampleData)element.DataContext;
int index = dataSource.IndexOf(item);
string s = "/Page"+index;
s +=".xaml";
NavigationService.Navigate(new Uri("s", UriKind.Relative));
}
catch
{
MessageBox.Show("Sorry..please try again..");
}
}
This is the part of the code. What is wrong in this? It goes to the navigation failed function when I click on an item in the list box. I want to get the item pressed and add it to the navigation page. Could you help please?
Remove the quotes around "s":
NavigationService.Navigate(new Uri(s, UriKind.Relative));
If you put s
, then you're using the contents of the variable s
. If you use "s"
, then you're creating a new string containing the literal value s
.
Looking at the way you are getting index from datasource I feel, may be you are trying to pass a QueryString argument to your page..if so, try like this
string s = "/Page.xaml?index=" + index;
and in your destination page load, read that argument
string qryArgVal = "";
NavigationContext.QueryString.TryGetValue("index", out qryArgVal);
i found myself the answer.in a listbox the simplest way of getting the index is int k = ((ListBox)sender).SelectedIndex;
and now your k will have the index value
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.