I have listbox in my WP7 that uses the below DataTemplete to display the list items
<DataTemplate x:Key="MetaDataTemplate">
<Grid Width="440" Margin="4,12,0,12">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="64"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu>
<toolkit:MenuItem Header="download" Visibility="{Binding DownloadVisible}" Command="{Binding Download}"/>
<toolkit:MenuItem Header="get link" Command="{Binding GetLink}"/>
<toolkit:MenuItem Header="delete" Command="{Binding Delete}"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
<Image Height="64" Width="64" Source="{Binding Thumb}" Stretch="UniformToFill" />
<TextBlock Text="{Binding MetaData.Name, Mode=OneWay}" VerticalAlignment="Center" Margin="12,0,0,0"
Style="{StaticResource MetaDataHeaderStyle}" Grid.Column="1" />
</Grid>
</DataTemplate>
defining the context menu data template makes it quite impossible to close the menu on pressing the back button! Does anyone faced this problem? How did you solve it?
I searched for the solution on internet, but couldn’t find one. Any help is appreciated.
A way to do it is to have a
ContextMenuvariable in the code behind, and have an event handler for theOpenedevent of the ContextMenu in the template.In the
Openedevent handler, set the ContextMenu variable to that instance (i.e. sender).Finally, override OnBackKeyPress, so that if the variable is not null, and the menu is open, then close the menu and cancel the back event.
That should do it! Let me know if you have any issues, or need me to paste the full code.