简体   繁体   English

WPF 文本框在系统托盘的上下文菜单中显示时不允许输入

[英]WPF Textbox does not allows input when it is displayed in context menu on system tray

I want to provide search functionality on context menu displayed in the system tray.我想在系统托盘中显示的上下文菜单上提供搜索功能。 I am using System.Windows.Forms.NotifyIcon class to display the context menu in the tray.我正在使用System.Windows.Forms.NotifyIcon class 在托盘中显示上下文菜单。 Basically this search should allow the user to find the required menu from the context menu.基本上,这个搜索应该允许用户从上下文菜单中找到所需的菜单。 For that, I added the WPF textbox like this in the below image, but it does not allow me to type the text.为此,我在下图中添加了 WPF 文本框,但它不允许我输入文本。

Is there any way to handle the text input on this?有没有办法处理这个文本输入?

在此处输入图像描述

Here is my code, MainWindow.xaml这是我的代码, MainWindow.xaml

<Window x:Class="SysTray_Sample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:SysTray_Sample"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <ContextMenu x:Key="NotifierContextMenu"

                    Placement="MousePoint">
            <MenuItem Header="First" StaysOpenOnClick="True"/>
            <MenuItem StaysOpenOnClick="True">
                <MenuItem.Header>
                    <TextBox Text="Test" Width="120" />
                </MenuItem.Header>
            </MenuItem>
            
            <MenuItem Header="Open" Click="Menu_Open"/>
            <MenuItem Header="Close" Click="Menu_Close"/>

        </ContextMenu>
    </Window.Resources>
    <Grid>
        <Button Height="22" Width="180" Content="Right click over me">
            <Button.ContextMenu>
                <ContextMenu>
                    <MenuItem Click="MenuItem_Click" Header="Minimize to system tray"/>
                    <MenuItem Header="Desktop mode"/>
                    <TextBox Width="150"/>
                </ContextMenu>
            </Button.ContextMenu>
        </Button>
    </Grid>
</Window>

MainWindow.xaml.cs主窗口.xaml.cs

public partial class MainWindow : Window
    {
        private System.Windows.Forms.NotifyIcon _trayIcon;

        public MainWindow()
        {
            InitializeComponent();
            _trayIcon = new System.Windows.Forms.NotifyIcon();
            _trayIcon.MouseDown += MyNotifyIcon_MouseDown;
            _trayIcon.Icon = new System.Drawing.Icon(@"Resources\add.ico");
            _trayIcon.MouseDoubleClick += MyNotifyIcon_MouseDoubleClick;
        }

        private void MyNotifyIcon_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                ContextMenu menu = (ContextMenu)this.FindResource("NotifierContextMenu");
                menu.IsOpen = true;
            }
        }

        private void MyNotifyIcon_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            this.WindowState = WindowState.Normal;
            _trayIcon.Visible = false;
            this.ShowInTaskbar = true;
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            this.ShowInTaskbar = false;
            _trayIcon.BalloonTipTitle = "Minimize Sucessful";
            _trayIcon.BalloonTipText = "Minimized the app ";
            _trayIcon.ShowBalloonTip(400);
            _trayIcon.Visible = true;
            this.WindowState = WindowState.Minimized;
        }

        private void Menu_Open(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Open");
        }

        private void Menu_Close(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Close");
        }
    }

After a long hour of searching, I found the following solution which worked for my case.经过一小时的搜索,我找到了以下适用于我的案例的解决方案。 https://www.codeproject.com/Questions/184429/Text-box-is-not-working-in-WPF-Popup https://www.codeproject.com/Questions/184429/Text-box-is-not-working-in-WPF-Popup

[DllImport("USER32.DLL")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        public static void ActivateVisual(FrameworkElement contextMenu)
        {
            var fromVisual = HwndSource.FromDependencyObject(contextMenu);
            HwndSource source = (HwndSource)fromVisual;
            IntPtr handle = source.Handle;

            SetForegroundWindow(handle);
        }

Here is my complete MainWindow.xaml.cs code,这是我完整的MainWindow.xaml.cs代码,

public partial class MainWindow : Window
    {
        private System.Windows.Forms.NotifyIcon _trayIcon;

        [DllImport("USER32.DLL")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        public static void ActivateVisual(FrameworkElement contextMenu)
        {
            var fromVisual = HwndSource.FromDependencyObject(contextMenu);
            HwndSource source = (HwndSource)fromVisual;
            IntPtr handle = source.Handle;

            SetForegroundWindow(handle);
        }

        public MainWindow()
        {
            InitializeComponent();
            _trayIcon = new System.Windows.Forms.NotifyIcon();
            _trayIcon.MouseDown += MyNotifyIcon_MouseDown;
            _trayIcon.Icon = new System.Drawing.Icon(@"C:\Users\nambi\source\repos\SysTray_Sample\SysTray_Sample\Resources\add.ico");
            _trayIcon.MouseDoubleClick += MyNotifyIcon_MouseDoubleClick;
        }

        private void MyNotifyIcon_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                ContextMenu menu = (ContextMenu)this.FindResource("NotifierContextMenu");                
                menu.IsOpen = true;
                ActivateVisual(this.ContextMenu);
            }
        }

        private void MyNotifyIcon_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            this.WindowState = WindowState.Normal;
            _trayIcon.Visible = false;
            this.ShowInTaskbar = true;
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            this.ShowInTaskbar = false;
            _trayIcon.BalloonTipTitle = "Minimize Sucessful";
            _trayIcon.BalloonTipText = "Minimized the app ";
            _trayIcon.ShowBalloonTip(400);
            _trayIcon.Visible = true;
            this.WindowState = WindowState.Minimized;
        }



        private void Menu_Open(object sender, RoutedEventArgs e)
        {
            ActivateVisual(this);
            MessageBox.Show("Open");
        }



        private void Menu_Close(object sender, RoutedEventArgs e)
        {
            ActivateVisual(this);
            MessageBox.Show("Close");
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 输入@时在文本框中弹出上下文菜单 - Pop context menu in textbox when @ is input C#WPF MVVM - 系统托盘图标上下文菜单中的文本框 - C# WPF MVVM - textbox in system-tray icon contextmenu C#系统托盘上下文菜单显示在图标下方 - C# system tray context menu showing below the icon WPF - 按钮上下文菜单未正确显示 - WPF - Button context menu not displayed properly 在WPF中的菜单上执行操作时,为什么TextBox不会失去焦点? - Why a TextBox does not lose the focus when acting over a menu in WPF? 消息框时C#托盘上下文菜单未隐藏 - C# tray context menu not hiding when message box C# 当托盘图标不在同一个表单上时,如何将上下文菜单分配给托盘图标? - C# How can I assign a context menu to a tray icon when the tray icon is not on the same form? 单击WPF中的桌面快捷方式时,从系统托盘还原应用程序 - Restore application from system tray when clicking on desktop shortcut in wpf 枚举文件夹/子文件夹内容,右键单击C#中系统托盘应用程序的上下文菜单 - Enumerate folder/subfolder contents to right-click context menu of system tray application in C# 如何在系统托盘上下文菜单中使用Windows look&#39;n&#39;feel? - How can I use the Windows look'n'feel for a system tray context menu?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM