2012-07-16 160 views
1

是否可以将上下文菜单附加到wpf控件,并在左键单击时打开它(而不是更常见的右键单击)?我只想用xaml来实现这一点(这应该是我的控件的视图模板的一部分)。wpf上下文菜单左键单击

回答

0
  1. 创建以编程方式打开子菜单的方法,因为这SO文章中指出: Show menu programmatically in WPF

  2. 创建LeftMouseButtonDown事件并调用该事件在XAML。

+0

不会从代码违反视图和逻辑之间的分离吗?对我来说这部分属于视图(所以应该在xaml中指定)。 – 2012-07-17 20:45:21

+0

这是作为视图一部分的视图的代码隐藏。你所指的是关于在ViewModel中实现这个代码,我根本没有建议。 – Xcalibur37 2012-07-17 22:06:34

+0

好吧,就这样吧。但仍然,我不明白“为LeftMouseButtonDown创建事件并调用该方法”部分。我在Generic.xaml中有一个用于控制的样式。当我点击这种风格中定义的控件之一时,我想要显示上下文菜单。图像控制。所以我试图将MouseUp =“method”添加到Image标签中,但是我得到:error MC4007:无法在样式中的Target标签上指定事件'MouseUp'。改用EventSetter。如何在风格中连接事件处理程序?我的方法应该放在哪里? Generic.xaml.cs? – 2012-07-18 20:18:39

4

这里是我会怎么做的一个简单的例子,我的建议:

的XAML:

<Window x:Class="LeftClickMenu.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow"> 
    <Grid> 
     <Border Width="400" Height="300" Background="#ccc" BorderBrush="#333" 
       BorderThickness="1" 
       MouseLeftButtonDown="Border_MouseLeftButtonDown" 
       MouseRightButtonUp="Border_MouseRightButtonUp"> 
      <Border.ContextMenu> 
       <ContextMenu x:Name="myContextMenu"> 
        <MenuItem Header="Menu Item 1" /> 
        <MenuItem Header="Menu Item 2" /> 
        <MenuItem Header="Menu Item 3" /> 
        <MenuItem Header="Menu Item 4" /> 
        <MenuItem Header="Menu Item 5" /> 
       </ContextMenu> 
      </Border.ContextMenu> 
     </Border> 
    </Grid> 
</Window> 

和代码隐藏:

using System.Windows; 
using System.Windows.Input; 

namespace LeftClickMenu 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
     { 
      myContextMenu.IsOpen = true; 
     } 

     private void Border_MouseRightButtonUp(object sender, MouseButtonEventArgs e) 
     { 
      e.Handled = true; 
     } 
    } 
} 

我还增加了额外的MouseRightButtonUp事件来禁止右键单击c弹出上下文菜单。

+0

(对不起,延迟回复。)但是,如何将这种行为转换为样式(例如在Generic.xml中)。正如我以前的评论中指出的那样,我得到了“错误MC4007:事件'MouseUp'不能在样式中的Target标签上指定,而是使用EventSetter。” – 2012-07-25 18:26:53

+0

我同意这个错误。您应该使用事件设置器。我不相信有一种有效的方式可以做到这一点,因为您需要在特定事件中发生这种情况。 – Xcalibur37 2012-07-26 15:59:47

4

这里是显示在左侧单击上下文菜单的方式:

Border元素上创建一个新的左按钮处理程序:

<Border x:Name="Win" 
     Width="40" 
     Height="40" 
     Background="Purple" 
     MouseLeftButtonUp="UIElement_OnMouseLeftButtonUp"> 

再补充一点:

private void UIElement_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
{ 
    e.Handled = true; 

    var mouseDownEvent = 
     new MouseButtonEventArgs(Mouse.PrimaryDevice, 
      Environment.TickCount, 
      MouseButton.Right) 
     { 
      RoutedEvent = Mouse.MouseUpEvent, 
      Source = Win, 
     }; 


    InputManager.Current.ProcessInput(mouseDownEvent); 
} 

它的功能,它基本上将左键单击到右键单击。为了可重用性,您可以将其包装为附加行为。

+0

谢谢,这是我发现的唯一解决方案,它尊重您在ContextMenuService.Placement ...属性中指定的内容。 – 2017-07-12 09:34:54

相关问题