SanthoshKumar SanthoshKumar - 10 days ago 5
C# Question

WPF Context Menu command Binding

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()
{

}

Answer

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