2

我有一个里面有 ContextMenu 的 ListBox。

XAML

<ListBox ItemsSource="{Binding}"
                         Name="recipesListBox"
                         HorizontalAlignment="Stretch"
                         VerticalAlignment="Stretch"
                         Grid.Row="1"
                         Margin="12,0,12,0"
                         Loaded="recipesListBox_Loaded"
                         SelectionChanged="recipesListBox_SelectionChanged" >
                    <ListBox.ItemContainerStyle>
                        <Style TargetType="ListBoxItem">
                            <Setter Property="HorizontalAlignment" Value="Stretch" />
                            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                        </Style>
                    </ListBox.ItemContainerStyle>
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Border Name="listBoxItemBoarder" BorderBrush="White" BorderThickness="1" Margin="0,0,0,10" 
                                    Background="Black" Height="118" HorizontalAlignment="Stretch" >
                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                                    <Canvas>
                                        <Image Source="{Binding Image}" Height="116" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
                                    </Canvas>
                                    <StackPanel Orientation="Vertical" HorizontalAlignment="Stretch" Margin="100,0,0,0">
                                        <TextBlock Text="{Binding Name}" FontWeight="Bold" FontSize="22" />
                                        <TextBlock Text="{Binding Category}" />
                                        <TextBlock Text="{Binding IngredientsStringWithoutAmounts}" Width="329" TextWrapping="Wrap" Height="56" TextTrimming="WordEllipsis" FontStyle="Italic" />
                                    </StackPanel>
                                    <toolkit:ContextMenuService.ContextMenu>
                                        <toolkit:ContextMenu Name="allCocktailsContextMenu">
                                            <toolkit:MenuItem Header="{Binding Path=AppResources.MainPage_PivotItem_AllCocktails_allCocktailsContextMenu_Header_AddFav, Source={StaticResource LocalizedStrings}}" Click="MenuItem_Click" />
                                            <toolkit:MenuItem Header="{Binding Path=AppResources.MainPage_PivotItem_AllCocktails_allCocktailsContextMenu_Header_MarkTried, Source={StaticResource LocalizedStrings}}" Click="MenuItem_Click" />
                                        </toolkit:ContextMenu>
                                    </toolkit:ContextMenuService.ContextMenu>
                                </StackPanel>
                            </Border>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

C#

private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            MenuItem menuItem = (MenuItem)sender;
            ListBoxItem selectedListBoxItem = this.recipesListBox.ItemContainerGenerator.ContainerFromItem((sender as MenuItem).DataContext) as ListBoxItem;
            string message = "You pressed " + menuItem.Header.ToString() + ".\n";
            try
            {
                Recipe selectedRecipe = selectedListBoxItem.DataContext as Recipe;
                message += "Selected recipe: " + selectedRecipe.Name + ", " + selectedRecipe.ID;
            }
            catch (NullReferenceException exc)
            {
                Debug.WriteLine(exc.ToString());
                message += "NullReferenceException :(";
            }
            MessageBox.Show(message, "Info", MessageBoxButton.OK);
        }

ListBox 在我的应用程序的 MainPage 中,当我尝试检索与 ContextMenu 绑定的对象时,一切正常,但是,当我执行页面导航并返回我的 MainPage 后,如果我再次尝试检索对象,我得到NullReferenceException。我在 WP 编码方面真的很年轻,而且我没有使用 MVVM 模式,这是我的错误吗?

4

1 回答 1

0

您要在哪个对象上获得 Null?它可能是你的 DataContext 但没有错误的对象很难说

于 2012-06-06T18:15:09.280 回答