views:

35

answers:

0

So I have a context menu in a TreeView and it wasn't enabling the MenuItem because CommandParameter was always null.

What I discovered is that CanExecute gets called before PlacementTarget is set and therefore before CommandParamater changes.

So in TargetUpdated I force the command to fire CanExecuteChanged.

<ContextMenu>
  <MenuItem Header="Delete"
            Command="{Binding PlacementTarget.DataContext.Delete, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"
            CommandParameter="{Binding PlacementTarget.DataContext, Mode=OneWay, NotifyOnTargetUpdated=true, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"
            TargetUpdated="MenuItem_TargetUpdated"/>
</ContextMenu>