如何在文本块中显示列表框中选中的项目

如何在文本块中显示列表框中选中的项目

问题描述:

我有一个列表框,其中使用绑定来填充数据,我想在出现在下一页中的文本块中的选定列表项中显示名称和年龄。如何在文本块中显示列表框中选中的项目

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> 

我试图做这样,

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

但我不明白如何从NavigationEventArgs E中的值在一个文本块显示名称和Age在另一个文本块中。
中的数据被通过这个绑定..

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; 
+1

触发选项的参数更改事件并使用参数更新文本块文本。 – VenkyDhana

+0

Rachel, 您是否将ListBox与某些数据源绑定? 对我来说(从你的给定的代码),列表框是空的。 对不对? –

+0

是的,列表框中的数据是通过数据绑定显示的。 – Rachel

我找到了答案,

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

并在接下来的页面中,得出这样

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