views:

159

answers:

1

I have a context menu, that I'd like to change the Header based on whether the Control Key is pressed or not.

Right now I have,

<MenuItem Header="Send To">
  <MenuItem ... />
  <MenuItem ... />
</MenuItem>

I'd like based on the Control Key being down to be,

<MenuItem Header="Move To">
  <MenuItem ... />
  <MenuItem ... />
</MenuItem>

All I really need to do is change the Header text, because inside the code I know how to check for the Modifier key being Control.

A: 

Bind the Header to some property in your view model, and have it change (with notification) when the modifier key is pressed.

Aviad P.
I've tried this Aviad, but I can't seem to get it to work. I can update the title of the application, and it switches back and forth with control being down/up. But the header in the context menu doesn't change, in fact it is blank. Here is what I have.<ContextMenu DataContext="{Binding ElementName=winMain, Path=myContextMenu}" KeyDown="KeyDown_Handler" KeyUp="KeyUp_Handler"><MenuItem Header="{Binding Path=SendTo, UpdateSourceTrigger=PropertyChanged}">...myContextMenu is a class that echo's property change events for the SendTo propertyDo I have the binding coded correctly?
Chuck Savage
Nop, the binding is not correct, it's a XAML namespace issue. The object referenced to by the `ContextMenu` property of whatever element it's applied to, does not belong to the namespace of the window. Therefore you can't use `ElementName` binding on it and have it refer to the window.
Aviad P.
Some more clarifications: The `ContextMenu` cannot paricipate in any logical or visual hierarchy, therefore it does not inherit any `DataContext` nor is it part of the namespace of its so-called 'container' since it's not really its container after all, at least not until the user invokes it. When that happens, its `DataContext` is set to its container's `DataContext` but only if it doesn't have a local value assigned to its own `DataContext` property, which in your case it does. What you'd expect is for WPF to assign the `DataContext` first, and then evaluate the binding, but thatsnotthecase
Aviad P.