0

嗨,这是使用 xaml 页面,我想使用选定的项目导航。

 <ListBox x:Name="NotchsList11"  Margin="0,0,0,0" Grid.Row="3" HorizontalAlignment="left" Width="Auto"  Grid.RowSpan="2">
    <ListBox.ItemTemplate>
      <DataTemplate> 
         <StackPanel VerticalAlignment="Top" Width="Auto">
            <ListBox ItemsSource="{Binding Images}"  Width="Auto" ScrollViewer.VerticalScrollBarVisibility="Disabled" SelectionChanged="NotchsList11_SelectionChanged">
              <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                       <StackPanel Orientation="Horizontal"  VerticalAlignment="Top">
                       </StackPanel>
                    </ItemsPanelTemplate>
              </ListBox.ItemsPanel>
              <ListBox.ItemTemplate>
                <DataTemplate>
                    <Image Source="{Binding}" Width="152" Height="90"  Stretch="Fill"  VerticalAlignment="Top">
                   </Image>
                </DataTemplate>
             </ListBox.ItemTemplate>
           </ListBox>
        </StackPanel>
      </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

我的 xaml.cs 页面代码如下

private void NotchsList11_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Notch selectedItemData = (sender as ListBox).SelectedItem as Notch;
        if(selectedItemData != null)
        {
            NavigationService.Navigate(new Uri(string.Format("/Test.xaml?parameter={0}",selectedItemData.articleid), UriKind.Relative));
        }
    }

如果我选择任何一项我无法导航到其他页面,因为我收到错误 selectedItemData is null.so 任何人都可以帮助我吗?

4

2 回答 2

0

一个。

        private void NotchsList11_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Notch selectedItemData  = NotchsList11.SelectedItem as Notch;
        if (selectedItemData != null)
        {
            NavigationService.Navigate(new Uri(string.Format("/Test.xaml?parameter={0}",selectedItemData.articleid), UriKind.Relative));
        }
    }

B. 或者您可以使用整个 SelectedItemData 对象作为数据上下文

private void NotchsList11_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Notch selectedItemData  = NotchsList11.SelectedItem as Notch;
        if (selectedItemData != null)
        {
            NavigationService.Navigate(new Uri("/Test.xaml", UriKind.Relative));
            FrameworkElement root = Application.Current.RootVisual as FrameworkElement;
            root.DataContext = selectedItemData;
        }
    }

备注:在Test页面中可以使用(Notch)DataContext来引用Class的实例,这样就可以使用Image数据作为((Notch)DataContext).Image

C. 或者,这样:

        private void NotchsList11_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (e.AddedItems.Count > 0)
        {
            NavigationService.Navigate(new Uri(((Notch)e.AddedItems[0]).articleid, UriKind.Relative));

            ((ListBox)sender).SelectedIndex = -1;

备注:Notch 是您作为 ItemSource 绑定到 ListBox 的类

最好的问候斯帕索

于 2013-03-27T22:40:19.367 回答
0

不要尝试从发件人对象中提取。如果实际上代码认为某个项目确实被选中并且不为空,则只需直接从列表对象中提取您需要的内容。

您只需将您的 SelectionChanged 方法更改为如下所示:

    private void NotchsList11_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (NotchsList11.SelectedItem != null)
        {
            NavigationService.Navigate(new Uri(string.Format("/Test.xaml?parameter={0}",
                (NotchsList11.SelectedItem as Notch).articleid), UriKind.Relative));
        }
    } 

这是一个示例: http ://code.msdn.microsoft.com/wpapps/Windows-Phone-8-JumpStart-1b7c34e3/sourcecode?fileId=72994&pathId=1694015380

于 2013-03-27T03:07:09.757 回答