简体   繁体   中英

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

 }

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>

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM