EddieDuy
EddieDuy

Reputation: 61

Passing selected item in listbox as a string to another page

I have the listbox like this... What I want is when I select an item in listbox... it will passing the selected value as a string to another page. HOw can I get string of the selected item and pass that value to next page?

<ListBox x:Name="AnyList" ItemsSource="{Binding LoadSearch1}" SelectionChanged="AnyList_SelectionChanged">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Grid Width="466" Margin="0, 0, 0, 12">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="10"/>
                                    <ColumnDefinition Width="360"/>
                                </Grid.ColumnDefinitions>
                                <Grid Grid.Column="0"></Grid>
                                <StackPanel Grid.Column="1">
                                    <TextBlock FontSize="40"   Text="{Binding ByAny}" FontWeight="Normal" FontStyle="Normal" Style="{StaticResource PhoneTextTitle3Style}" TextWrapping="Wrap"/>
                                </StackPanel>                                   
                            </Grid>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

And this is my next page:

 protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        string any = NavigationContext.QueryString["passingvalue"];
        base.OnNavigatedTo(e);
        App.MainViewModel.SearchAny(any);
    }

I have tried this but unsuccessful.....

private void AnyList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        string ListBoxConent = ((ListBoxItem)AnyList.SelectedItem).Content.ToString();
        NavigationService.Navigate(new Uri("/View/SearchResult/SearchAny.xaml?passingvalue=" + ListBoxConent, UriKind.RelativeOrAbsolute));
    }

I get this error: System.InvalidCastException: Unable to cast object of type 'Search' to type 'System.Windows.Controls.ListBoxItem'

My LoadSearch1 is:

private ObservableCollection<Search> _LoadSearch1 = new ObservableCollection<Search>();
    public ObservableCollection<Search> LoadSearch1
    {
        get { return _LoadSearch1; }
        set
        {
            _LoadSearch1 = value;
            NotifyPropertyChanged("LoadSearch1");
        }
    }

And I have add data to LoadSearch1 by:

public void AddSearch1(string newhistory)
    {
        LoadSearch1.Add(new Search() { ByAny = newhistory });      
    }

. And this is my Search class:

public class Search
    {
        private string _ByAny;
        public string ByAny
        {
            get { return _ByAny; }
            set
            { _ByAny = value; }
        }
        private string _ByTitle;
        public string ByTitle
        {
            get { return _ByTitle; }
            set
            { _ByTitle = value; }
        }
}

Upvotes: 0

Views: 1722

Answers (4)

Archana
Archana

Reputation: 376

public class ImageProperty {

    public string ImageUrl { get; set; }
    public string ImageSrc { get; set; }
}

private void LstImage_SelectionChanged(object sender, SelectionChangedEventArgs e) {

            ListBox listBox = (sender as ListBox);
            if (listBox != null)
            {
                imageProperty = (listBox.SelectedItem) as ImageProperty;
                if (imageProperty != null)
                {
                    string LstItem = imageProperty.ImageUrl;

                    NavigationService.Navigate(new Uri("/View/ImageSelector.xaml?ImageUri=" + LstItem + "", UriKind.RelativeOrAbsolute));
                }
            }

    }

Upvotes: 1

har07
har07

Reputation: 89285

What you get in AnyList.SelectedItem is based on type of its ItemsSource. You need to change this line :

string ListBoxConent = ((ListBoxItem)AnyList.SelectedItem).Content.ToString();

to this :

string ListBoxConent = ((MainViewModel.Search)AnyList.SelectedItem).ByAny;

Upvotes: 1

Obiwan007
Obiwan007

Reputation: 646

If you have the value of the selected item of the listbox (get it via listbox.SelectedItem) you could just navigate to your nextpage

NavigationService.Navigate(new Uri("/Views/YourView.xaml"+"?passingvalue="+yourstringvalue, UriKind.Relative));

Of cause you could only submit strings via that method. But I think it is also passing objects vie NavigationService. Another option would be to reference the selected object in the params and get it back in the NavigatedTo event from a central store...

Upvotes: 0

System Down
System Down

Reputation: 6260

Depends on what you want to do and how you want it done. Basically there are three ways:

  • In the query string, also known as a GET HTTP request. The value becomes part of the URL for the second page (www.mysite.com/second_page.aspx?key=value).

  • In the request body, also known as a POST HTTP request. The value is a hidden field inside the first page that gets submitted to the second page.

  • As a session variable. The value is stored in the server (either in memory or in a database, depending on how you configure it) and can be accessed by the code of the second page.

Each method has it's pros and cons and complete articles have been written on the difference between them and how to implement them. It's far too much information to be contained in a single answer. Just Google these terms and you should find plenty of resources, and if you run into problems or have further questions you are more than welcome to ask them here in StackExchange.

Upvotes: 0

Related Questions