3

标题几乎说明了一切。我有很多按钮,它们都有相同的上下文菜单,我想从点击事件中确定哪个按钮被点击到那里。

此代码不起作用,placementTarget 为空:

private void mi_Click(object sender, RoutedEventArgs e)
{
        Button contextMenuEzen = null;

        MenuItem mnu = sender as MenuItem;

        if (mnu != null)
        {
            ContextMenu ize =(ContextMenu)mnu.Parent;
            contextMenuEzen = ize.PlacementTarget as Button;
        }
}

请帮我!

4

3 回答 3

3

对我来说,这个例子有效:

XAML

<Window.Resources>
    <!-- For all MenuItems set the handler -->
    <Style TargetType="{x:Type MenuItem}">
        <EventSetter Event="Click" Handler="MenuItem_Click" />
    </Style>

    <!-- One ContextMenu for all buttons (resource) -->
    <ContextMenu x:Key="MyContextMenu">
        <MenuItem Header="Click this" />
        <MenuItem Header="Two" />
        <MenuItem Header="Three" />
    </ContextMenu>
</Window.Resources>

<Grid>
    <Button x:Name="MyButton1" Width="100" Height="30" Content="MyButton1" ContextMenu="{StaticResource MyContextMenu}" />

    <Button x:Name="MyButton2" Margin="0,110,0,0" Width="100" Height="30" Content="MyButton2" ContextMenu="{StaticResource MyContextMenu}" />
</Grid>

Code behind

private void MenuItem_Click(object sender, RoutedEventArgs e)
{
    MenuItem mnu = sender as MenuItem;
    Button MyButton = null;

    if (mnu != null)
    {
        ContextMenu MyContextMenu = (ContextMenu)mnu.Parent;
        MyButton = MyContextMenu.PlacementTarget as Button;
    }

    MessageBox.Show(MyButton.Content.ToString());
}
于 2013-08-16T09:24:05.063 回答
1

RoutedEvents 的工作方式与常规事件不同 - 处理程序的签名是 EventHandler(object sender, RoutedEventArgs e)。RoutedEventArgs 有一个名为 OriginalSource 的属性,它表示 UI 中实际被点击的元素。相反,sender 参数将始终是注册事件处理程序的对象。

于 2013-08-15T19:59:53.100 回答
0

使用ContextMenuService获取放置目标,如下例所示:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Loaded += (sender, args) => {

            RoutedEventHandler clickHandler = delegate(object o, RoutedEventArgs eventArgs) {
                var mi = (MenuItem) o;
                var contextMenu = (ContextMenu) mi.Parent;

                var pTarget = ContextMenuService.GetPlacementTarget(contextMenu) as Button;

                // just to make sure
                if (pTarget == null) {
                    return;
                }

                string message = "You clicked on the button " + pTarget.Content;
                MessageBox.Show(message);
            };

            // create a single instance of the ContextMenu
            var cm = new ContextMenu();
            for (int i = 0; i < 10; i++) {
                var mi = new MenuItem {Header = "Item " + i};
                mi.Click += clickHandler;
                cm.Items.Add(mi);
            }

            // create a set of buttons and assign them to the RootVisual(StackPanel)
            for (int i = 0; i < 5; i++) {
                var button = new Button {Content = "Button " + i, ContextMenu = cm};
                this.RootVisual.Children.Add(button);
            }
        };
    }
}
于 2013-08-18T21:55:06.017 回答