在子菜单WPF中获得专注的MenuItem [英] Get focused MenuItem in submenu WPF

查看:584
本文介绍了在子菜单WPF中获得专注的MenuItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用包含子菜单的菜单编写一个应用程序。另外我有一个StatusBar,当用户在带有键盘的菜单中导航时,我想要显示有关集中MenuItem的信息。
我试图从每个MenuItem处理GotFocus事件,并将StatusBar的内容更改为发件人的标签,但它只能与MenuItem 1一起使用,而不是1.1和2.2。

XAML:

 <菜单高度=23x:Name =mainMenu> 
< MenuItem Header =Header1Tag =Info1GotFocus =MenuItem_GotFocus_1>
< MenuItem Header =Header1.1Tag =Info1.1GotFocus =MenuItem_GotFocus_1/>
< MenuItem Header =Header1.2Tag =Info1.1GotFocus =MenuItem_GotFocus_1/>
...
< / MenuItem>
...
< / Menu>

C#:

  private void MenuItem_GotFocus_1(object sender,RoutedEventArgs e)
{
statusBarItem.Content =(发件人为FrameworkElement).Tag;



$ b $ p
$ b

如何显示有关子菜单的重点项目的信息?也许有其他方法可以做到吗?

感谢Aleksandr。

不知道它是否适用于你需要的东西,但我认为这是你需要的...



最好绑定到视图模型 - 然后你可以通过简单的绑定来暴露这个状态...
$ b

如果 IsFocused (如果你正在谈论标准的WPF菜单项)有一个小问题绑定到它,因为它是一个只读的绑定失败,像一些像
http://meleak.wordpress.com/2011/08/28/onewaytosource- binding-for-readonly-dependency-property /

(这也是这个解决方案的一个很好的例子,类似于ActiveWidth / Height)

 < TreeView.ItemContainerStyle> 
< Style TargetType ={x:Type TreeViewItem}>
< Setter Property =IsExpandedValue ={Binding IsExpanded,Mode = TwoWay}/>
< Setter Property =IsSelectedValue ={Binding IsSelected,Mode = TwoWay}/>
< Setter Property =pb:PushBindingManager.StylePushBindings>
< Setter.Value>
< pb:PushBindingCollection>
< pb:PushBinding TargetProperty =IsFocusedPath =IsFocused/>
< / pb:PushBindingCollection>
< / Setter>

您可以下载项目/ lib以支持上述文章中的链接(<$ c
放置类似于code> xmlns:pb =clr-namespace:PushBindingExtension; assembly = some-assembly
(我有它复制,集成,所以我没有确切的来源/命名在这里)。

你应该开始走了。只要在你的视图模型中使用 IsFocused ,把菜单绑定到它上面,然后把任何项目放在之下在状态。这里需要一些腿部工作来达到这个目的,但是很少。



希望这有助于注意:使用其他链接下载(即 http://dl.dropbox.com/u/39657172/Blog/PushBindingInStyleDemo.zip ) >
(其中一个包含您需要的 StylePushBindings ,对于样式。


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?

Thanks, Aleksandr.

解决方案

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...

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>

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).

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.

Hope this helps

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆