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