在XP中左键单击图像的菜单不会打开

时间:2011-05-06 06:16:51

标签: c# .net wpf c#-4.0

我正在尝试使用左键单击鼠标时显示菜单的图像(在WPF中)。它在7中运行良好,但是,当我尝试在XP中使用它时它只会在右键单击时出现?任何想法?

以下是XAML代码:

<Image x:Name="buttonHelp" Width="32" Height="32"  Margin="0,0,5,0" Source="/Little Registry Optimizer;component/Resources/Help.png" MouseEnter="button_MouseEnter" MouseLeave="button_MouseLeave" MouseDown="buttonHelp_MouseLeftButtonDown" />

这是C#代码:

private void button_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
    this.Cursor = Cursors.Hand;
}

private void button_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
    this.Cursor = Cursors.Arrow;
}

private void buttonHelp_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    ContextMenu contextMenu = new ContextMenu();
    this.buttonHelp.ContextMenu = contextMenu;

    contextMenu.Items.Add(CreateMenuItem(Properties.Resources.Help, "Help"));
    contextMenu.Items.Add(new Separator());
    contextMenu.Items.Add(CreateMenuItem(Properties.Resources.internet, "Visit Website"));
    contextMenu.Items.Add(CreateMenuItem(Properties.Resources.update, "Check for updates"));
    contextMenu.Items.Add(new Separator());
    contextMenu.Items.Add(CreateMenuItem(Properties.Resources.optimizer, "About..."));

    contextMenu.PlacementTarget = (System.Windows.UIElement)sender;
    contextMenu.IsOpen = true;
    contextMenu.StaysOpen = true;
}

谢谢!

1 个答案:

答案 0 :(得分:0)

通过使用按钮而不是图像,您知道我已经解决了这个问题。您仍然可以将按钮设置为将图像作为其内容。然后你可以简单地订阅Buttons Click事件(我知道它可以在XP或其他任何东西上工作)。

我做的另一件事是使用以下代码设置上下文菜单的位置:

buttonHelp.ContextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
ContextMenuService.SetPlacement(buttonHelp, System.Windows.Controls.Primitives.PlacementMode.Bottom);