I have several textblocks functioning as tiles. What I want to do is to tap and hold on any of the textblocks and get a contextmenu. This I have done and this works fine. Where I am stuck is when I tap on a menuitem inside the contextmenu, I want to know on which textblock the menuitem was tapped. How can I achieve that?
I have already tried something like this but without luck:
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
MenuItem menuItem = (MenuItem)sender;
var sndr = menuItem.ItemContainerGenerator.ContainerFromItem((sender as ContextMenu).DataContext).ToString();
// var sndr = menuItem.DataContext.ToString();
MessageBox.Show("you tapped at " + sndr);
}
This is the xaml code where the contextmenu and tetblocks are located:
<ScrollViewer>
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu Name="holdOptions">
<toolkit:MenuItem Header="New" Click="MenuItem_Click" />
<toolkit:MenuItem Header="Delete" Click="MenuItem_Click"/>
<toolkit:MenuItem Header="Clear" Click="MenuItem_Click"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="208" />
<ColumnDefinition Width="5" />
开发者_Go百科 <ColumnDefinition Width="208" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="210"/>
<RowDefinition Height="5" />
<RowDefinition Height="210" />
<RowDefinition Height="5" />
<RowDefinition Height="210" />
</Grid.RowDefinitions>
<StackPanel Grid.Column="0" Grid.Row="0" Background="{StaticResource PhoneAccentBrush}" Height="205" Width="205">
<TextBlock Height="205" Width="205" Name="con1" Text="Tap to add" Tap="con1_Tap" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Center" TextAlignment="Center" FontSize="28" Hold="con1_Hold" />
</StackPanel>
<StackPanel Grid.Column="2" Grid.Row="0" Background="{StaticResource PhoneAccentBrush}" Height="205" Width="205">
<TextBlock Height="205" Width="205" Name="con2" Text="Tap to add" Tap="con2_Tap" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Center" TextAlignment="Center" FontSize="28" Hold="con2_Hold" />
</StackPanel>
<StackPanel Grid.Column="0" Grid.Row="2" Background="{StaticResource PhoneAccentBrush}" Height="205" Width="205">
<TextBlock Height="205" Width="205" Name="con3" Text="Tap to add" Tap="con3_Tap" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Center" TextAlignment="Center" FontSize="28" Hold="con3_Hold" />
</StackPanel>
<StackPanel Grid.Column="2" Grid.Row="2" Background="{StaticResource PhoneAccentBrush}" Height="205" Width="205">
<TextBlock Height="205" Width="205" Name="con4" Text="Tap to add" Tap="con4_Tap" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Center" TextAlignment="Center" FontSize="28" Hold="con4_Hold" />
</StackPanel>
</Grid>
</ScrollViewer>
The only solution I have found that works is to attach a separate context menu to each TextBlock.
<StackPanel Grid.Column="0" Grid.Row="0" Background="{StaticResource PhoneAccentBrush}" Height="205" Width="205">
<TextBlock Height="205" Width="205" Name="con1" Text="Tap to add" Tap="con1_Tap" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Center" TextAlignment="Center" FontSize="28" Hold="con1_Hold"/>
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu Name="holdOptions">
<toolkit:MenuItem Header="New" Click="MenuItem_Click" />
<toolkit:MenuItem Header="Delete" Click="MenuItem_Click"/>
<toolkit:MenuItem Header="Clear" Click="MenuItem_Click"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</TextBlock>
</StackPanel>
<StackPanel Grid.Column="2" Grid.Row="0" Background="{StaticResource PhoneAccentBrush}" Height="205" Width="205">
<TextBlock Height="205" Width="205" Name="con2" Text="Tap to add" Tap="con2_Tap" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Center" TextAlignment="Center" FontSize="28" Hold="con2_Hold" >
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu Name="holdOptions">
<toolkit:MenuItem Header="New" Click="MenuItem_Click" />
<toolkit:MenuItem Header="Delete" Click="MenuItem_Click"/>
<toolkit:MenuItem Header="Clear" Click="MenuItem_Click"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</TextBlock>
</StackPanel>
Then you can just use
var menuItem = ((MenuItem)sender);
It's far from ideal but as long as you don't have too many items that you want to attach the context menu to its not too bad, although if anyone has a better solution I'd like to know it too.
精彩评论