We are using a Toolbox:ListPicker control to display an ObservableCollection. We now want to let users optionally navigate through the ListPicker to an editor for the objects in the ObservableCollection.
We added a ContextMenu to the ItemTemplate allowing the user to Navigate to the editor. The user is presented with the context menu after they long-tap on an item. If the user taps on the menu item they are presented with the editor page. After the user presses the back button in the editor, they are returned to a completely blank page - only the system tray is shown. If the user presses the back button again, the ListPicker briefly appears before begin replaced by the page hosting the ListPicker instance.
What is the point of the blank page and is there a way to get rid of it?
Here is the XAML for the ListPicker instance.
<toolkit:ListPicker
x:Name="listPicker_Generators"
Margin="12,-6,0,12"
ExpansionMode="FullScreenOnly"
HorizontalAlignment="Left"
Width="430"
Height="Auto"
VerticalAlignment="Top"
FullModeHeader="{Binding Source={StaticResource LocalizedStrings}, Path=LocalizedResources.label_AccountEditor_Generator}"
>
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Name}" FontSize="36" />
<TextBlock Text="{Binding Notes}" Margin="0,0,0,12" FontFamily="Segoe WP" FontSize="{StaticResource PhoneFontSizeSmall}" />
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu
Visibility="{Binding Visibility}"
Closed="ContextMenu_Closed"
Opened="ContextMenu_Opened">
<toolkit:MenuItem
Name="menuItem_Edit"
Header="{Binding Source={StaticResource LocalizedStrings}, Path=LocalizedResources.label_AccountEditor_EditGenerator}"
Click="menuItem_Edit_Click" />
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>
Here is the code behind for the context menu handling.
private void menuItem_Edit_Click(object sender, RoutedEventArgs e)
{
AccountViewModel.GeneratorChoice item = ((AccountViewModel.GeneratorChoice)((sender as FrameworkElement).DataContext));
if (item != null)
{
AccountViewModel vm = DataContext as AccountViewModel;
vm.HoldingSelectedGeneratorIndex = listPicker_Generators.SelectedIndex;
NavigationService.Navigate(new Uri("/Views/GeneratorEditor.xaml?id=" + item.Id.ToString(), UriKind.Relative));
// force the selection to change so we get redrawn when we come back
if (listPicker_Generators.SelectedIndex > 0)
listPicker_Generators.SelectedIndex--;
else
listPicker_Generators.SelectedIndex++;
}
}
private void ContextMenu_Closed(object sender, RoutedEventArgs e)
{
AccountViewModel vm = DataContext as AccountViewModel;
vm.ContextMenuOpen = false;
}
private void ContextMenu_Opened(object sender, RoutedEventArgs e)
{
// a work-around from http://stackoverflow.com/questions/15181441/windows-phone-toolkit-context-menu-items-have-wrong-object-bound-to-them-when-an
ContextMenu contextMenu = (sender as ContextMenu);
FrameworkElement owner = (contextMenu.Owner as FrameworkElement);
if (owner.DataContext != contextMenu.DataContext)
contextMenu.DataContext = owner.DataContext;
AccountViewModel.GeneratorChoice item = contextMenu.DataContext as AccountViewModel.GeneratorChoice;
if (item.Id.Value != 0)
{
bool factorySupplied = item.FactorySupplied == null ? false : true;
if (factorySupplied)
{
contextMenu.Items.OfType<MenuItem>().First(m => (string)m.Name == "menuItem_Edit").Header = AppResources.label_AccountEditor_ViewGenerator;
}
AccountViewModel vm = DataContext as AccountViewModel;
vm.ContextMenuOpen = true;
}
}