Get focused MenuItem in submenu WPF

前端 未结 1 713
醉酒成梦
醉酒成梦 2021-01-22 11:31

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 ke

相关标签:
1条回答
  • 2021-01-22 12:06

    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.

    0 讨论(0)
提交回复
热议问题