WPF上下文菜单单击问题 [英] WPF Context Menu Click issue

查看:71
本文介绍了WPF上下文菜单单击问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了WPF用户控件来加载到Excel任务窗格中。当我单击上下文菜单中的第一个菜单项时,事件将被触发。但是,该事件不会被其他菜单项触发。代码如下:


    <用户控件X:类= QUOT; Controls.TabBrowser"

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP; xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation " ; $
                 xmlns:x =" http://schemas.microsoft.com/winfx/2006/xaml "

                 xmlns:mc =" http://schemas.openxmlformats.org/markup-compatibility/2006 "


                 xmlns:d =" http://schemas.microsoft.com/expression/blend/2008 "


                 MC:可忽略= QUOT; d"


                的xmlns:自我= QUOT; CLR-名称空间:控制与QUOT;

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;了minHeight = QUOT; 25" MinWidth = QUOT; 500" MaxWidth =" 1265">

        < Grid x:Name =" MyGrid">

    < Button Name =" MyButton"含量="按一下[宽度= QUOT; 100"的Horizo​​ntalAlignment = QUOT;左" PreviewMouseRightButtonDown =" MyButton_PreviewMouseRightButtonDown"&
                  &NBSP;&NBSP; < Button.ContextMenu>

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP; <文本菜单>&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; < MenuItem Name =" cmDuplicateWB"点击= QUOT; cmDuplicateWB_Click"&NBSP;
ToolTip ="复制所选工作簿"头= QUOT;重复"图标= [储存" />

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; < MenuItem Name =" cmRenameWB"点击= QUOT; cmRenameWB_Click"
ToolTip ="重命名所选工作簿"头= QUOT;重命名"图标= [储存"标签= QUOT; F2" />

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; < MenuItem Name =" cmShowFolder"点击= QUOT; cmShowFolder_Click"
ToolTip ="在文件资源管理器中打开文件"标题="在文件夹中显示"图标= [储存" />&NBSP;&NBSP;&NBSP;

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; < /文本菜单>

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ; < /Button.ContextMenu>&
                < / Button>

        < / Grid>

    < / UserControl>


   使用System.Windows.Controls;

   使用System.ComponentModel;

   使用系统;

   使用System.Windows;
$
   使用System.Windows.Input;

   

   命名空间控件

    {

        ///< summary>

        /// TabBrowser.xaml的交互逻辑

        ///< / summary>

        public partial class TabBrowser:UserControl,INotifyPropertyChanged

        {

     

            public TabBrowser()

            {

               的InitializeComponent();&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;


            }
   

            private void cmDuplicateWB_Click(object sender,RoutedEventArgs e)

            {

                MessageBox.Show(" Duplicate");

            }
   

            private void cmRenameWB_Click(object sender,RoutedEventArgs e)

            {

                MessageBox.Show(" Rename");

   

                }
       

               私人无效cmShowFolder_Click(对象发件人,RoutedEventArgs E)

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; MessageBox.Show(" Show in Folder");

                }
       

        private void MyButton_PreviewMouseRightButtonDown(object sender,MouseButtonEventArgs e)

                {

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; this.MyButton.Focus();

                }


           



解决方案


我有一个测试,它可以正常工作。


我使用的是带有office2016 64bit和VS2015Enterprise的win10 64位。


我使用与您相同的代码,这是我的步骤:


  1. 添加参考System.Xaml(或者我将得到3个错误:在名称空间'System.Windows.Markup'中找不到类型名称'IComponentConnector'。)
  2. 添加用户控件(WPF)
  3. 构建
  4. 添加用户控件
  5. 将用户控件(WPF)从工具箱拖到任务窗格的设计器
  6. 重建

你还可以吗?分享我们如何添加您的用户控件,我建议您在winform等其他应用程序中测试用户控制以检查我 f有任何问题。



问候,


Edward


I have created WPF user control to load into the Excel taskpane. When I click the first menu item in the context menu, the event is getting triggered. However, the event is not getting triggered for the other menu item. The code is as follows:

    <UserControl x:Class="Controls.TabBrowser"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                 mc:Ignorable="d"
                 xmlns:self="clr-namespace:Controls"
                 MinHeight="25" MinWidth="500" MaxWidth="1265">
        <Grid x:Name="MyGrid">
    <Button Name="MyButton" Content="Click" Width="100" HorizontalAlignment="Left" PreviewMouseRightButtonDown="MyButton_PreviewMouseRightButtonDown">
                    <Button.ContextMenu>
                        <ContextMenu>                           
                                <MenuItem Name="cmDuplicateWB" Click="cmDuplicateWB_Click"  ToolTip="Duplicate the selected workbook" Header="Duplicate" Icon="Save"/>
                                <MenuItem Name="cmRenameWB" Click="cmRenameWB_Click" ToolTip="Rename the selected workbook" Header="Rename" Icon="Save" Tag="F2"/>
                                <MenuItem Name="cmShowFolder" Click="cmShowFolder_Click" ToolTip="Open the file in the file explorer" Header="Show in Folder" Icon="Save"/>   
                    </ContextMenu>
                    </Button.ContextMenu>
                </Button>
        </Grid>
    </UserControl>

    using System.Windows.Controls;
    using System.ComponentModel;
    using System;
    using System.Windows;
    using System.Windows.Input;
   
    namespace Controls
    {
        /// <summary>
        /// Interaction logic for TabBrowser.xaml
        /// </summary>
        public partial class TabBrowser : UserControl, INotifyPropertyChanged
        {
     
            public TabBrowser()
            {
                InitializeComponent();           
            }
   
            private void cmDuplicateWB_Click(object sender, RoutedEventArgs e)
            {
                MessageBox.Show("Duplicate");
            }
   
            private void cmRenameWB_Click(object sender, RoutedEventArgs e)
            {
                MessageBox.Show("Rename");
   
                }
       
                private void cmShowFolder_Click(object sender, RoutedEventArgs e)
                {
                    MessageBox.Show("Show in Folder");
                }
       
        private void MyButton_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
                {
                    this.MyButton.Focus();
                }

           

解决方案

Hi,

I have a test and it works correctly.

I am using win10 64bit with office2016 64bit and VS2015Enterprise.

I am using the same codes as you and here are my steps:

  1. Add Reference System.Xaml (or I will get 3 errors: The type name 'IComponentConnector' could not be found in the namespace 'System.Windows.Markup'.)
  2. Add a User Control(WPF)
  3. Build
  4. Add a User Control
  5. Drag the User Control(WPF) from toolbox to the designer of the task pane
  6. Rebuild

So could you share us how you add your User Control, I suggest you to test User Control in other application like winform to check if there are any problems.

Regards,

Edward


这篇关于WPF上下文菜单单击问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆