今天我对我的WPF用户界面有了一些新的限制,消除了MenuBar的永久可见性。如何使WPF MenuBar visibile按ALT键时?
我想过模仿Windows Live Messenger的用户界面。只有在按下ALT键的情况下,该应用程序才会显示MenuBar。并且在MenuBar的焦点丢失时再次隐藏它。
目前我没有线索如何在WPF中构建这样的事情...是这样的可能吗?
在此先感谢。
今天我对我的WPF用户界面有了一些新的限制,消除了MenuBar的永久可见性。如何使WPF MenuBar visibile按ALT键时?
我想过模仿Windows Live Messenger的用户界面。只有在按下ALT键的情况下,该应用程序才会显示MenuBar。并且在MenuBar的焦点丢失时再次隐藏它。
目前我没有线索如何在WPF中构建这样的事情...是这样的可能吗?
在此先感谢。
你可以写在主窗口中的一个关键按下事件..
KeyDown="Window_KeyDown"
和代码隐藏文件..
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.LeftAlt || e.Key == Key.RightAlt)
{
myMenu.Visibility = Visibility.Visible;
}
}
如果你想与MVVM或使用绑定来实现这一目标
...哟你可以使用输入键绑定
<Window.InputBindings>
<KeyBinding Key="LeftAlt" Command="{Binding ShowMenuCommand}"/>
<KeyBinding Key="RightAlt" Command="{Binding ShowMenuCommand}"/>
</Window.InputBindings>
这是我的理解:
private void form_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode == Keys.Alt && /*menu is not displayed*/)
{
// display menu
}
}
private void form_KeyUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if (/*check if mouse is NOT over menu using e.X and e.Y*/)
{
// hide menu
}
}
如果你需要不同的东西打了一下用键盘和鼠标事件。
看起来不像WPF我 – CodesInChaos
可以使用的KeyDown(或预览版本,如果你喜欢),然后检查是否有这样的系统关键:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.KeyDown += new KeyEventHandler(MainWindow_KeyDown);
}
void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.System && (Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)
{
// ALT key pressed!
}
}
}
我认为正确的实现是用KeyUp。这是IE8,Vista中,Windows7的等近期微软产品的行为:
private void MainWindow_KeyUp(Object sender, KeyEventArgs e)
{
if (e.Key == Key.System)
{
if (mainMenu.Visibility == Visibility.Collapsed)
mainMenu.Visibility = Visibility.Visible;
else
mainMenu.Visibility = Visibility.Collapsed;
}
}
这确实是系统的关键。 –
猜猜这是仅次于作业的代码。 MVVM确实适合,但不应该用于这个我猜。试过了,它工作。但是,在所有的答案中,我都怀念菜单栏将再次崩溃的部分。 –
您可以使用LostFocus或LostKeyboardFocus事件来处理折叠菜单... – Bathineni
我已经使用了上述解决方案。也增加了Key.System。但是,所有LostFocus事件似乎都不能正常工作。只有MenuBar.IsMouseCaptureWithinChanged事件才能正确执行作业。 –