在子菜单 WPF 中获取焦点 MenuItem [英] Get focused MenuItem in submenu WPF
问题描述
我正在编写一个带有包含子菜单的菜单的应用程序.我还有一个 StatusBar,当用户使用键盘在菜单中导航时,我想在其中显示有关聚焦 MenuItem 的信息.我尝试处理来自每个 MenuItem 的 GotFocus 事件并将 StatusBar 的内容更改为发件人的标签,但它仅适用于 MenuItem 1,不适用于 1.1 和 2.2.
I'm writing an application with menu containing submenus. Also I have a StatusBar where I want to display information about focused MenuItem when user navigates in menu with keyboard. I tried to handle GotFocus event from each MenuItem and change StatusBar's content to sender's Tag, but it works only with MenuItem 1, not with 1.1 and 2.2.
XAML:
<Menu Height="23" x:Name="mainMenu">
<MenuItem Header="Header1" Tag="Info1" GotFocus="MenuItem_GotFocus_1">
<MenuItem Header="Header1.1" Tag="Info1.1" GotFocus="MenuItem_GotFocus_1"/>
<MenuItem Header="Header1.2" Tag="Info1.1" GotFocus="MenuItem_GotFocus_1"/>
...
</MenuItem>
...
</Menu>
C#:
private void MenuItem_GotFocus_1(object sender, RoutedEventArgs e)
{
statusBarItem.Content = (sender as FrameworkElement).Tag;
}
如何显示有关子菜单的焦点项目的信息?也许还有其他方法可以做到?
How can I display info about submenu's focused items? Maybe are there other ways to do it?
谢谢,亚历山大.
推荐答案
不确定它是否完全适用于您所需要的,但我认为这正是您所需要的...
Not sure if it applies to what you need exactly but I think it's what you need...
绑定到视图模型总是最好的 - 然后您可以通过简单地绑定到它来在其他地方公开该状态"......
It's always best to bind to view-model - and then you can expose that 'status' at some other place by simply binding to it...
如果是 IsFocused
(如果你在谈论标准的 WPF 菜单项)绑定到它有一个小问题,因为它是只读的,所以绑定失败,比如
http://meleak.wordpress.com/2011/08/28/onewaytosource-binding-for-readonly-dependency-property/
(这也是这个解决方案的一个很好的例子,类似于 ActiveWidth/Height)
In case of IsFocused
(If you're talking about the standard WPF menu items) there is a slight problem binding to it, as it's a read-only so binding fails with something like
http://meleak.wordpress.com/2011/08/28/onewaytosource-binding-for-readonly-dependency-property/
(that's also a good example of this solution, similar just for ActiveWidth/Height)
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
<Setter Property="pb:PushBindingManager.StylePushBindings">
<Setter.Value>
<pb:PushBindingCollection>
<pb:PushBinding TargetProperty="IsFocused" Path="IsFocused"/>
</pb:PushBindingCollection>
</Setter.Value>
</Setter>
您可以从上面文章中的链接下载项目/lib 以支持它 (PushBindingManager
)放一些类似 xmlns:pb="clr-namespace:PushBindingExtension;assembly=some-assembly"
的东西(我已经复制、集成了它,所以我这里没有确切的来源/命名).
You can download the project/lib to support that from the link in the article above (PushBindingManager
)
Put something like xmlns:pb="clr-namespace:PushBindingExtension;assembly=some-assembly"
(I have it copied, integrated so I don't have the exact source/naming here).
你应该准备好了.只需在您的视图模型中制作 IsFocused
,将菜单绑定到它 - 然后将任何 聚焦
项放在状态.这里需要一些腿部工作"来实现这一点,但非常少.
And you should be set to go. Just make IsFocused
in your view-model, bind the menu to it - and then put up that whatever item is focused
at the status. There is some 'leg work' required here to get this going but pretty minimal.
希望能帮到你
注意:使用其他链接下载(即 http://dl.dropbox.com/u/39657172/Blog/PushBindingInStyleDemo.zip)
(那个包含您需要的用于样式的 StylePushBindings
.
NOTE: use the other link for download (i.e. http://dl.dropbox.com/u/39657172/Blog/PushBindingInStyleDemo.zip)
(that one contains the StylePushBindings
which you need, for styles.
这篇关于在子菜单 WPF 中获取焦点 MenuItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!