I have a button that I want to display a pop-up menu when the user left-clicks on it. I have implemented the pop-up as the button's ContextMenu. The availability of the menu items in the contextmenu needed to be controlled through bindings to various ViewModel properties. As you know, ContextMenus exist on a separate visual tree than the parent window, so I had to do a bit of research to figure out how to set the DataContext of the ContextMenu to be same as the datacontext of the parent control (i.e. the button). Here is how I did that:
<Button Name="AddRangeBtn" Height="50" Width="50" Margin="5"
**Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType={x:Type StackPanel}}}">**
<Button.Content>
<Image Source="{StaticResource DmxPlusIconImage}"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
MaxHeight="50" MaxWidth="50" Margin="-10" />
</Button.Content>
<Button.ToolTip>
<ToolTip Style="{DynamicResource DMXToolTipStyle}"
Content="{x:Static Localization:StringResources.AddFrequencyRangeTooltip}"/>
</Button.ToolTip>
<Button.ContextMenu>
**<ContextMenu Name="AddRngContextMenu"
DataContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">**
<MenuItem Style="{StaticResource localMenuItem}"
IsEnabled="{Binding CanAddLinearRange}">
<MenuItem.ToolTip>
<ToolTip Style="{DynamicResource DMXToolTipStyle}"
Content="{x:Static Localization:StringResources.ToolTip_AddLinearFrequencyRange}"/>
The trick to setting the datacontext was binding the datacontext of the contextmenu to the PlacementTarget.Tag in the contextmenu and adding the corresponding Tag property in the buttton. With that in place, I could then directly bind properties in the MenuItems (like the IsEnabled="{Binding CanAddLinearRange}") to ViewModel's dependancy properties. This all worked fine when I access the contextmenu by right-clicking on the button, but I want the menu to display with a normal left-click. So, I added the following EventTrigger to the button:
<Button.Triggers>
<EventTrigger SourceName="AddRangeBtn" RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="AddRngContextMenu" Storyboard.TargetProperty="(ContextMenu.IsOpen)">
<DiscreteObjectKeyFrame KeyTime="0:0:0">
<DiscreteObjectKeyFrame.Value>
<system:Boolean>True</system:Boolean>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
With this, the contextmenu does show up with a left-click on the button, but that instance of the contextmenu doesn't appear to be getting the same datacontext as the instance that is displayed with a right-click!!! In the left-click version all the items in the menu are enabled, so obviously, the contextmenu is not seeing the dependency properties from the viewmodel. I don't know how this is even possible that there appears to be two different versions of the contextmenu, one with the correct datacontext and one with the default behavior.
ContextMenu when launched with right-click:
ContextMenu when launched with left-click:
I am using XAML to create the left-click behavior and in some of the example I've seen where people have used code-behind to implement the left-click context menu, they have rest the datacontext of the contextmenu to the button.datacontext in the button.click event handler. I am guessing that is what I need to do as well, but I am not sure how to do it in the xaml.
Any ideas would be highly appreciated.

