, . , -, . , MouseRightButtonUp, , , . Private Label , . MenuItem Label. , , , MouseRightButtonUp.
:
<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>
:
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;
}