I’ve a ListBox with a ContextMenu inside.
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);
}
The ListBox is in the MainPage of my app, and when I try to retrieve the object binded with ContextMenu everything works fine but, when I perform a page navigation and after go back to my MainPage, if I try to retrieve an object again I get a NullReferenceException. I’m really young in WP coding, and I’m not using the MVVM pattern, is this my error?
Which object are you getting a Null on? It may be your DataContext but without the erred object it’s hard to tell