4

Is it possible to bind the IsEnabled property of a button to a context menu item that is represented by a NotifyIcon?

When I push the menu item, it starts a method that disables my btnSave. In that case, I would like to "turn off" the MenuItem too. I attempted it this way, but it's not working:

<Window.Resources>
    <ContextMenu x:Key="NotifierContextMenu" Placement="MousePoint">
        <MenuItem Header="Start" Click="start_timer" IsEnabled="{Binding ElementName=btnSave, Path=IsEnabled}"/>
    </ContextMenu>
</Window.Resources>
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Webapache
  • 97
  • 1
  • 6

1 Answers1

5

I believe your problem stems from a ContextMenu not existing within the visual tree, so ElementName bindings will be unsuccessful, unless you take some additional steps to correct the Name Scope, or resolve the DataContext to the visual tree.

Most of the techniques are covered fairly comprehensively in this answer: ElementName Binding from MenuItem in ContextMenu.

I've had success in the past with binding to a named element by reference, in your case, that would look something like this:

    <MenuItem Header="Start" 
              IsEnabled="{Binding IsEnabled, Source={x:Reference btnSave}}"
              Click="btnSave_Click"/>
Community
  • 1
  • 1
Chris
  • 8,268
  • 3
  • 33
  • 46
  • Glad it helped, I'd definitely give that other answer a read for some alternatives if you have some spare time =D – Chris May 03 '15 at 14:55