0

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; }
        }
}
EddieDuy
  • 61
  • 2
  • 10
  • The question is still not very clear. On which part you have problem: 1. *How to navigate to other page?* 2. *How to pass value/parameter from one page to another?* 3. *How to get string from selected item in listbox?*. Answer to each of those question maybe related, so if you already have solution for one of them please include it in the question. – har07 Dec 31 '13 at 03:45
  • possibly useful reference, if your actual question is the number 2: http://stackoverflow.com/a/12444817/2998271 – har07 Dec 31 '13 at 03:47
  • @har07 I want to get string from selected item in listbox and pass that string to the next page. As I edited in my question, u can see my next page, I want the passing string is contained in "passingvalue" – EddieDuy Dec 31 '13 at 10:38
  • Codes showing how you navigate from current page to the next page is more relevant then how you get query string in the next page, please include it if you already have one – har07 Dec 31 '13 at 11:04
  • @har07 I have posted my try.... pls check it – EddieDuy Dec 31 '13 at 11:14
  • you are pretty close now. of what type are items in LoadSearch1? – har07 Dec 31 '13 at 11:17
  • you need to change this part: `((ListBoxItem)AnyList.SelectedItem).Content.ToString()` to `((TypeOfItemInLoadSearch1)AnyList.SelectedItem).ByAny` – har07 Dec 31 '13 at 11:20
  • @har07 so, pls check Question again and what is "TypeOfItemInLoadSearch1" I should have? – EddieDuy Dec 31 '13 at 11:31
  • Back to my previous question, what is the type of LoadSearch1? I can only answer your question if you answer this – har07 Dec 31 '13 at 11:33
  • I have add LoadSearch1 into the question... – EddieDuy Dec 31 '13 at 11:36
  • Check my answer, and let me know whether it worked or not – har07 Dec 31 '13 at 11:39
  • @har07 it does not work...coz Search can not call in that. Search just is a public class in myViewMOdel so can not call it here – EddieDuy Dec 31 '13 at 11:51

4 Answers4

1

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;
har07
  • 88,338
  • 12
  • 84
  • 137
1

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));
                }
            }

    }
Archana
  • 376
  • 2
  • 12
0

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.

System Down
  • 6,192
  • 1
  • 30
  • 34
0

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...

Obiwan007
  • 646
  • 1
  • 8
  • 20