1

我有索姆列表框,我需要索姆帮助从x:Name =“ThisID”上的SelectionChanged获取文本。SelectionChanged获取文本块文本/ ID

我已经做了类似(发件人为列表框).SelectedItem但不止如此,我不知道该怎么做。

<ListBox ItemsSource="{Binding}" x:Name="ListBoxD" SelectionChanged="ListBoxD_SelectionChanged" toolkit:TiltEffect.IsTiltEnabled="True" Margin="10,0,0,0"> 
<ListBox.ItemTemplate><DataTemplate> 
<StackPanel Orientation="Horizontal" Margin="0,0,0,10"> 
<Border Width="80" Height="80" VerticalAlignment="Top" Background="{StaticResource PhoneAccentBrush}" Margin="0,5,0,0" Padding="5,0,5,10"> 
    <TextBlock Text="{Binding DeliveryNumber}" Foreground="{StaticResource PhoneContrastBackgroundBrush}" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="72" /> 
</Border> 
<StackPanel x:Name="StackPanelD" Orientation="Vertical" Margin="10,0,0,0"> 
    <TextBlock x:Name="ThisID" Text="{Binding ID}" Visibility="Collapsed"/> 
    <TextBlock Text="{Binding Name}"/> 
    <TextBlock TextWrapping="Wrap" FontSize="23" FontWeight="Bold" Text="{Binding AddressLine}"/>        
    </StackPanel> 

</StackPanel></DataTemplate> 
</ListBox.ItemTemplate> 
</ListBox> 

private void ListBoxDeliveryTo_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
NavigationService.Navigate(new Uri("/Page.xaml?ID=" + ID, UriKind.Relative)); 
} 

回答

0

比方说,你ListBox势必是MyCustomItem类型的项目,它具有以下属性的集合:DeliveryNumberIDName(因为我可以从你的DataTemplate告诉)。

SelectionChanged发生,你是,事实上,能够直接获取了所选择的项目:

MyCustomItem item = (MyCustomItem)lstItems.SelectedItem; 

然后,你可以得到它的名字。确保检查所选项目是否为空。

0

嗨让我们试试这个在FirstPage.xamal如果u有这样

<ListBox x:Name="FirstListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}" SelectionChanged="FirstListBox_SelectionChanged" > 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <StackPanel> 
          <TextBlock Text="{Binding Data}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" /> 
         </StackPanel> 
        </DataTemplate> 
       </ListBox.ItemTemplate></ListBox> 

而且在Firtpage.xaml.cs

 private void FirstListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     NavigationService.Navigate(new Uri("/newmessage.xaml?selectedItem=" + FirstListBox.SelectedIndex, UriKind.Relative)); 
     FirstListBox.SelectedIndex = -1; 
    } 

一些代码,现在去Secondpage.xaml

网格 加入收藏

<TextBox x:Name="textbox1" HorizontalAlignment="Left" Text="{Binding Data}" /> 

in Secondpage.xaml.cs添加该代码

int index = 0; 
    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
    { 

     string selectedIndex = ""; 
     if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex)) 
     { 
      index = int.Parse(selectedIndex); 
      DataContext = App.ViewModel.Items[index]; 
     } 
     base.OnNavigatedTo(e); 

    }