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!
PropertyEditorViewModelis 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-b56153PresentationTraceSources.TraceLevel=Highto 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