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

    }