SelectionChanged获取Textblock文本/ ID

时间:2013-03-15 11:55:07

标签: windows-phone-7 listbox selectionchanged navigationservice

我有一个som列表框,我需要帮助来获取来自x的文本:Name =“ThisID”在SelectionChanged上。

我做过类似的事情(发送者为ListBox).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));
}

2 个答案:

答案 0 :(得分:0)

假设您的ListBox绑定了MyCustomItem类型的项目集合,这些项目具有以下属性:DeliveryNumberID,{{1} (正如我从Name所说的那样)。

DataTemplate发生时,您实际上可以直接检索所选项目:

SelectionChanged

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

答案 1 :(得分:0)

嗨让我们试试这个在FirstPage.xamal如果你有像这样的代码

 <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}" />

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

    }