简体   繁体   English

我有一个由6个不同标签共享的上下文菜单,如何判断哪个标签正在使用上下文菜单的当前实例?

[英]I have a context menu which is being shared by 6 different labels, how can I tell which label is using the current instance of the context menu?

Here is the xaml for the contextMenu: 这是contextMenu的xaml:

    <Window.Resources>
    <ContextMenu x:Key="IBContextMenu" x:Shared="true" Name="IBContextMenu1">
        <MenuItem Header="Edit" Click="ibEdit_Click" AllowDrop="False" />
        <MenuItem Header="Clear" Click="ibClear_Click"/>
    </ContextMenu>
</Window.Resources>

Both the edit and clear items' methods need to know which label to act upon. 编辑和清除项目的方法都需要知道要对哪个标签进行操作。 How can I do this? 我怎样才能做到这一点?

I think you are looking for PlacementTarget : http://msdn.microsoft.com/en-us/library/system.windows.controls.contextmenu.placementtarget.aspx 我想你正在寻找PlacementTargethttp//msdn.microsoft.com/en-us/library/system.windows.controls.contextmenu.placementtarget.aspx

If you switch over to a Command-pattern, you can actually get this via Binding and pass it along as the CommandParameter... 如果切换到Command模式,您实际上可以通过Binding获取它并将其作为CommandParameter传递...

Here's an answer I came up with. 这是我想出的答案。 I don't really like it because it's a bit hack-ish, but it works. 我真的不喜欢它,因为它有点黑客,但它确实有效。 The idea is that you make your labels listen to the MouseRightButtonUp event, which is fired when the user releases the right mouse button after clicking to open the context menu. 这个想法是让你的标签监听MouseRightButtonUp事件,当用户在单击打开上下文菜单后释放鼠标右键时会触发该事件。 In the event handler, you set a private Label variable to the label that the user just right-clicked. 在事件处理程序中,您将私有Label变量设置为用户刚刚右键单击的标签。 Then, in your MenuItem click handler, you can access that private Label variable. 然后,在MenuItem单击处理程序中,您可以访问该私有Label变量。 Note that all the labels you want to do this must use the same event handler for MouseRightButtonUp. 请注意,您要执行此操作的所有标签必须使用MouseRightButtonUp的相同事件处理程序。

For example: 例如:

<Window.Resources>
    <ContextMenu x:Key="MyMenu">
        <MenuItem Header="Edit" Click="Edit_Click"/>
        <MenuItem Header="Clear" Click="Clear_Click"/>
    </ContextMenu>
</Window.Resources>
<StackPanel>
    <Label ContextMenu="{StaticResource MyMenu}" 
           MouseRightButtonUp="Label_MouseRightButtonUp">Some text</Label>
    <Label ContextMenu="{StaticResource MyMenu}" 
           MouseRightButtonUp="Label_MouseRightButtonUp">Some junk</Label>
    <Label ContextMenu="{StaticResource MyMenu}" 
           MouseRightButtonUp="Label_MouseRightButtonUp">Some stuff</Label>
    <Label ContextMenu="{StaticResource MyMenu}"
           MouseRightButtonUp="Label_MouseRightButtonUp">Some 0000</Label>
</StackPanel>

Code behind: 代码背后:

private void Edit_Click(object sender, RoutedEventArgs e)
{
    if (clickedLabel != null)
    {
        MessageBox.Show(clickedLabel.Content.ToString());
    }
}

private void Clear_Click(object sender, RoutedEventArgs e)
{
    if (clickedLabel != null)
    {
        MessageBox.Show(clickedLabel.Content.ToString());
    }
}

private Label clickedLabel;
private void Label_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
    clickedLabel = (Label)sender;
}

Try to set a DataContext to the Labels, for example 例如,尝试将DataContext设置为标签

And in the Click event just check the ((FrameworkElement)sender).DataContext for FIRST/SECOND etc. Let us know if that works. 在Click事件中,只需检查((FrameworkElement)发送者).DataContext for FIRST / SECOND等。如果有效,请告诉我们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM