2

I am having issues with using a ContextMenu within a ListBox. One of the items of my ContextMenu is used to pass the name of the item via querystring to another page in my application. As of now I am using the SelectionChanged event of my ListBox to retrieve the name of the item selected, but this requires that the user press and then release the item to register the event. I would like to be able to get the name of the item pressed when the user presses a ListBox item down to access the ContextMenu, without pressing up.

To Note, I have tried using the KeyDown event of the ListBox for this purpose, but it did not work either. What event can I use to meet this requirement?

4

1 回答 1

1

如果您在列表框中为列表框项使用绑定,则可以使用 ContextMenu 的 MenuItem 的标记属性。绑定标签使用

<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu Background="{StaticResource PhoneChromeBrush}">
<toolkit:MenuItem Foreground="{StaticResource PhoneForegroundBrush}" Header="Send" Name="Send" Tag="{Binding Name}" Click="Send_Click_1"/>
</toolkit:ContextMenuService.ContextMenu>

当您单击 ContextMenuItem 时,您可以使用

var name = ((MenuItem)sender).Tag.ToString();

如果您使用绑定属性,则可以使用此语句访问列表框项

var listBoxItemName= ((MenuItem)sender).DataContext as ListBoxItemClass;

在这里,ListBoxItemClass 是您用于绑定属性的类的名称。试试这个,希望你得到你想要的

于 2013-06-24T04:09:18.413 回答