1
votes

I am having a WPF binding issue that I cannot figure out. I have a ContextMenu template that is formatted as shown:

<ContextMenu x:Key="CopyPasteContextMenu">
    <MenuItem Header="AlternateDelete"
              Command="{Binding Path=PlacementTarget.Tag.DataContext.AlternateDeleteCommand, 
              RelativeSource={RelativeSource Self}, Mode=OneWay}"/>
</ContextMenu>

The context menu is being used in the DataTemplat, and the binding for the Tag on the Border is finding the PropertyEditorView correctly, I just can't get it from the border to the contextmenu.

<DataTemplate x:Key="PropertyValueCellViewingTemplate" DataType="viewModels:IConfigurationItemViewModel">
    <Border x:Name="ValueCellBorder"
           Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type views:PropertyEditorView}}}"
           ContextMenu="{StaticResource CopyPasteContextMenu}"
           Style="{StaticResource PropertyGridValueCellBorderStyle}">
        (...)
    </Border>
</DataTemplate>

The tag can bind properly to my view model which is called “PropertyEditorViewModel”. I can see this while debugging the system in the visual tree. When I drill into my Context Menu, the binding is not happening properly.

For my Command to work, I need it to bind properly to the Command to PropertyEditorView view model command called “AlternateDeleteCommand”.

public class PropertyEditorViewModel : DisposableViewModelBase, IPropertyEditorViewModel
{
    public ICommand AlternateDeleteCommand { get; set; }

Looked at this for a day so far, and not sure why my binding isn't working on the context menu, anyone got something I'm missing?

Thanks!

1
I just did a quick test, and found that the MenuItems are inheriting the DataContext of the target of the context menu -- e.g., I would expect that if a PropertyEditorViewModel is the DataContext of the DataTemplate, hence if the Border in the DataTemplate, then in the MenuItem, Command="{Binding AlternateDeleteCommand}" would work. - 15ee8f99-57ff-4f92-890c-b56153
When in doubt about why a binding is (or appears to be) failing, always add PresentationTraceSources.TraceLevel=High to it. That'll barf many lines of debugging information to the VS Output pane at runtime whenever the binding updates. It'll tell you exactly what it's doing to find its source property, and if it fails, it'll tell you where and why. - 15ee8f99-57ff-4f92-890c-b56153
I did try that with what JM suggested below and it worked, so I moved it back into the data template and it still worked once I set the relative source to the context menu. Thanks Ed! - Eric Gilchrist
I don't think you need any of that elaborate stuff I tested the case I showed you. You've got a very complicated way to bind to a property of the DataContext you already have, as far as I can see. - 15ee8f99-57ff-4f92-890c-b56153

1 Answers

0
votes

Doesn't the relative source need to be on the context menu and not on the menu item? Since you are checking the placement target of the context menu?

<MenuItem Header="AlternateDelete"
          Command="{Binding Path=PlacementTarget.Tag.DataContext.AlternateDeleteCommand, 
          RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Mode=OneWay}" />