简体   繁体   中英

How to display listbox selected item in a textblock

I have a listbox in which data is populated using binding, I want to display the Name and Age present in selected list item in to a textblock which is present in next page.

XAML :

<ListBox x:Name="listBoxobj" Background="Transparent" Height="388" Margin="22,0,0,0" SelectionChanged="listBoxobj_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <!--<Border BorderBrush="DarkGreen" BorderThickness="4">-->
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="auto"/>
                            <RowDefinition Height="auto"/>
                        </Grid.RowDefinitions>
                        <TextBlock  x:Name="txtName1" 
                                    Text="{Binding Name}" 
                                    Grid.Row="0" 
                                    Width="400" 
                                    Height="40"  
                                    Foreground="White" 
                                    FontSize="20" 
                                    Margin="8,0,-48,0"/>
                        <TextBlock x:Name="Age1" 
                                   Text="{Binding Age}" 
                                   Grid.Row="1" 
                                   Width="400" 
                                   Height="40"  
                                   Foreground="White" 
                                   FontSize="18" 
                                   Margin="8,0,-48,0"/>
                    </Grid>
                <!--</Border>-->
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

I tried doing in this way,

private void listBoxobj_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {         
        object  person = listBoxobj.SelectedItem;           
        Frame.Navigate(typeof(Page2), person);        
    }

But I don't understand how to get the values from NavigationEventArgs e to display the Name in one textBlock and Age in another textBlock.
The data is getting binded through this..

 ObservableCollection<MyData> Details = new ObservableCollection<MyData>();
        Details.Add(new MyData() { LineOne = "Teena", LineTwo = "20" });
        Details.Add(new MyData() { LineOne = "Riya", LineTwo = "21" });
        Details.Add(new MyData() { LineOne = "Priya", LineTwo = "22" });
        Details.Add(new MyData() { LineOne = "kila", LineTwo = "23" });
        this.listBoxobj.ItemsSource = Details;

I found the answer,

private void listBoxobj_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        MyData list_item = listBoxobj.SelectedItem as  MyData;    
        Frame.Navigate(typeof(Page2), list_item);        
    }

and in next Page, derive the parameters in this way

 protected override void OnNavigatedTo(NavigationEventArgs e)
    {
       textBlock1.Text = ((Appbar_Sample.MyData)e.Parameter).LineOne;
        textBlock2.Text = ((Appbar_Sample.MyData)e.Parameter).LineTwo;
    }

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