views:

105

answers:

3

Here is the xaml for the contextMenu:

    <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?

A: 
Jobi Joy
But how would you set the data context of the menu to the lablel?
Ian Oakes
You dont need, Whatever the DataContext of Label will pass to the MenuItem anyway.
Jobi Joy
I apologize if I take a while to respond, I haven't used data context before. Googling now.
Justin
I set the DataContext, but I don't know what I'm supposed to do now. I tried ((FrameworkElement)sender).DataContext.ToString, but I know that can't be right because it keeps returning: "MS.Internal.Data.XmlDataCollection"
Justin
this is not a good way to write software, it may work today but how will someone understand the code in a few years time or write tests for it?
Ian Ringrose
Is there another way I could do it?
Justin
+1  A: 

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. In the event handler, you set a private Label variable to the label that the user just right-clicked. Then, in your MenuItem click handler, you can access that private Label variable. Note that all the labels you want to do this must use the same event handler for 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;
}
Benny Jobigan
+3  A: 

I think you are looking for PlacementTarget: http://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...

JerKimball
This is the right answer, and doesn't require a work-around like mine. =)
Benny Jobigan
Thank you so much!
Justin