[英]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.