今天,我在我的WPF用户界面上得到了一些新的限制,这些限制应该会消除MenuBar的永久可见性。
我想要模仿Windows Live Messenger的用户界面。该应用程序仅在按下ALT-键时显示MenuBar。并在失去对MenuBar的关注时再次隐藏它。
目前我不知道如何在WPF中构建这样的东西……这样的事情有可能发生吗?
提前谢谢。
发布于 2011-07-13 21:17:50
你可以在主窗口中写下一个按键事件。
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>发布于 2011-07-13 21:49:21
我认为正确的实现是使用KeyUp。这是IE8,Vista,Windows7和其他最近的MS产品的行为:
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;
}
}发布于 2011-07-13 21:11:21
我的理解是:
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
}
}如果您需要一些不同的东西,可以使用键盘和鼠标事件。
https://stackoverflow.com/questions/6679197
复制相似问题