0

I have tried few solutions given in SO, but still i'm unable to trigger the command.

XAML:

 <Image Source="{Binding CurrentImage.Source, Mode=OneWay}" Grid.Row="0" Grid.Column="1" Tag="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType= Window}}">
     <Image.ContextMenu>
            <ContextMenu DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
                  <MenuItem Header="Edit Image" Command="{Binding PlacementTarget.Tag.EditImageCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"></MenuItem>
            </ContextMenu>
     </Image.ContextMenu>

ViewModel:

private ICommand _EditImageCommand;
public ICommand EditImageCommand
   {
      get
        {
           return _EditImageCommand ?? (_EditImageCommand = new CommandHandler(() => EditImage(), _canExecute));
        }
   }

 public void EditImage()
 {

 }
1
  • That binding via Tag and setting DataContext fills wrong (especially for MVVM). Check binding errors in Output window. It should be enough to set path only in Command binding normally. Commented Jul 27, 2016 at 12:02

3 Answers 3

2

Change:

private ICommand _EditImageCommand;
private ICommand EditImageCommand
   {
      get
        {
           return _EditImageCommand ?? (_EditImageCommand = new CommandHandler(() => EditImage(), _canExecute));
        }
   }

 public void EditImage()
 {

 }

to

private ICommand _EditImageCommand;
public ICommand EditImageCommand // has to be public
   {
      get
        {
           return _EditImageCommand ?? (_EditImageCommand = new CommandHandler(() => EditImage(), _canExecute));
        }
   }

 public void EditImage()
 {

 }

Commands have to be public to be accessed (or internal for the sake of correctness).

Also, change your xaml to:

<Image Source="{Binding CurrentImage.Source, Mode=OneWay}" Grid.Row="0" Grid.Column="1" Tag="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType= Window}}">
     <Image.ContextMenu>
            <ContextMenu DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
                  <MenuItem Header="Edit Image" Command="{Binding EditImageCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"></MenuItem>
            </ContextMenu>
     </Image.ContextMenu>
Sign up to request clarification or add additional context in comments.

2 Comments

Have changed, but still it doesn't.
Check my edit. Is your DataContext of the window you are in the viewmodel you posted? Why do you use the Relative source self?
1

Have changed my XAML to,

<Window.Resources>
        <local:ImageList x:Key="SliderViewModel"></local:ImageList>
</Window.Resources>

    <Image Source="{Binding CurrentImage.Source, Mode=OneWay}" Grid.Row="0" Grid.Column="1">
        <Image.ContextMenu>
               <ContextMenu>
                     <MenuItem Header="Edit Image" Command="{Binding EditImageCommand, Source={StaticResource SliderViewModel}}"></MenuItem>
                 </ContextMenu>
         </Image.ContextMenu>
     </Image>

Working fine. Thanks

1 Comment

Yes ofcourse it worked only after changing the command to public along with the XAML changes. Thanks you helped at right point.
1

Another nice workaround is to declare a static instance of your view model in App.xaml: <ViewModelTypeName x:Key="ViewModelName" d:IsDataSource="True" />and bind like this Command="{Binding Source={StaticResource ViewModelName}, Path=MyCommand}" I had the same issue when i needed to bind from Window and menuitem's native DataContext simultaneously. Also this solution looks not that complicated.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.