我有几个用作图块的文本块。我想要做的是点击并按住任何文本块并获取上下文菜单。我已经这样做了,这很好。我被困的地方是当我点击上下文菜单中的菜单项时,我想知道菜单项被点击的文本块。我怎样才能做到这一点?
我已经尝试过这样的事情,但没有运气:
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);
}
这是contextmenu和tetblock所在的xaml代码:
<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" />
<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>
答案 0 :(得分:0)
我找到的唯一解决方案是将单独的上下文菜单附加到每个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>
然后你可以使用
var menuItem = ((MenuItem)sender);
这远非理想,但只要你没有太多的项目,你想要将上下文菜单附加到它不太糟糕,虽然如果有人有更好的解决方案,我也想知道它。 / p>