简体   繁体   中英

Working with WP7 ListBox SelectedItem

While navigating to another page on Listbox SelectionChanged I want to pass the textblock value with it rather than sending the item index of listbox. How can I do that???

In order to get the selected ListBoxItem all you need to do is the following:

private void btnGetSelected_Click(object sender, RoutedEventArgs e)
{
    ListBoxItem selectedItem =this.listBox.ItemContainerGenerator.ContainerFromItem(this.listBox.SelectedItem) as ListBoxItem;
    var textblock = selectedItem.Content
}

source

Create a new DataBound Application.

Look at the generated source. It shows an example of how to do exactly this.

Try the following:

<ListBox x:Name="listBox" FontSize="26" SelectionChanged="listBox_SelectionChanged">
    <ListBoxItem Content="Item1"/>
    <ListBoxItem Content="Item2"/>
    <ListBoxItem Content="Item3"/>
    <ListBoxItem Content="Item4"/>
    <ListBoxItem Content="Item5"/>
    <ListBoxItem Content="Item6"/>
</ListBox>



        public void SurahsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string r = ((ListBox)sender).SelectedValue.ToString();
            NavigationService.Navigate(new Uri("/page.xaml?selecteItem=" + r, UriKind.Relative));
        }


protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            try
            {
                string selectedItem= "";
                if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedItem))
                {
                    if(null != selectedItem) {
                    // your code
                    }
                }
            }
            catch (Exception ex)
            {
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM