获取Button上下文菜单的所有者

时间:2013-08-15 19:07:15

标签: wpf wpf-controls

标题只是说明了一切。我有很多按钮,所有按钮都有相同的上下文菜单,我想从点击事件中确定哪个按钮被点击以便到达那里。

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

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;
        }
}

请帮助我!

3 个答案:

答案 0 :(得分:1)

RoutedEvents不像常规事件那样工作 - 处理程序的签名是EventHandler(对象发送者,RoutedEventArgs e)。 RoutedEventArgs有一个名为OriginalSource的属性,它表示实际单击的UI中的元素。相反,sender参数将始终是注册事件处理程序的对象。

答案 1 :(得分:1)

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

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());
}

答案 2 :(得分: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);
            }
        };
    }
}