0

I saw this SO question but when I tried it it does not work.

I know I can do mvvm but what the execute command be doing is view specific so I wanted it to be done on the view.

<Grid>
        <TextBox>
            <TextBox.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="_Copy" CommandTarget="{Binding Path=PlacementTarget, 
          RelativeSource={RelativeSource FindAncestor, 
          AncestorType={x:Type ContextMenu}}}">
                        <MenuItem.CommandBindings>
                            <CommandBinding CanExecute="CommandBinding_CanExecute"
                                                Executed="CommandBinding_Executed"
                                                />
                        </MenuItem.CommandBindings>
                    </MenuItem>
                </ContextMenu>
            </TextBox.ContextMenu>
        </TextBox>
    </Grid>

Code behind:

private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = false;
        }

I am expecting the menu to be disabled but it is not:

enter image description here

I tried other approaches:

<!--Approach 2: CopyCommand as property of the Window. CopyCommand is in code-behind of the window-->
<MenuItem Header="_Copy" Command="{Binding Path=CopyCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>

<!--Approach 3: Saw other samples on the net about binding to PlacementTarget. CopyCommand is in code-behind of the window-->
<MenuItem Header="_Copy" Command="{Binding Path=PlacementTarget.CopyCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
Community
  • 1
  • 1
Jan Navarro
  • 327
  • 4
  • 16

3 Answers3

0

If you are trying to implement WPF inbuilt ApplicationCommands.Copy property all you need to do is

<TextBox>
    <TextBox.ContextMenu>
        <ContextMenu>
                <MenuItem Command="ApplicationCommands.Copy"/>
        </ContextMenu>
    </TextBox.ContextMenu>
</TextBox>

Since the command is associated with the textbox it will automatically handle the execute and can execute events.

However if you want to implement your own custom command you have to be little bit verbose

public static class MyCommands
{
    public static readonly RoutedUICommand Copy= new RoutedUICommand
            (
                    "Copy",
                    "_Copy",
                    typeof(MyCommands),
                   null
            );

    //You can define more commands here
}

private void CommandBinding_CanExecute(object sender,  CanExecuteRoutedEventArgs e)
{
        e.CanExecute = true; //can check with debugger...
}

private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
     //Your Action   
}

in your xaml

<Window.CommandBindings>
    <CommandBinding Command="local:MyCommands.Copy" CanExecute="CommandBinding_CanExecute" Executed="CommandBinding_Executed" />
</Window.CommandBindings>
<Grid>

    <TextBox>
        <TextBox.ContextMenu>
            <ContextMenu>
                <MenuItem Command="local:MyCommands.Copy"/>
            </ContextMenu>
        </TextBox.ContextMenu>
    </TextBox>
</Grid>
Rohit
  • 10,056
  • 7
  • 50
  • 82
0

I have my CopyCommand in the view's code behind.

View:

public ICommand CopyCommand { get { return _copyCommand; } }

Then I was able to access it when I put the view's instance in the textbox's tag property as specified here

<views:myView>

<Grid>
<TextBox Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type views:myView}}}">
            <TextBox.ContextMenu>
                <ContextMenu DataContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
                        <MenuItem Header="_Copy" Command="{Binding CopyCommand}"/>
                    </ContextMenu>
            </TextBox.ContextMenu>
        </TextBox>
</views:myView>

</Grid>
Community
  • 1
  • 1
Jan Navarro
  • 327
  • 4
  • 16
-1

Specify Command in your MenuItem like this :

<MenuItem Command="ApplicationCommands.Copy" .../>

Also specify a value for Command property in the CommandBinding,

<MenuItem.CommandBindings>
     <CommandBinding Command="ApplicationCommands.Copy" CanExecute="CommandBinding_CanExecute"
                     Executed="CommandBinding_Executed" />
</MenuItem.CommandBindings>
AnjumSKhan
  • 9,647
  • 1
  • 26
  • 38
  • I tried but it did not work. I looked at Snoop and it contains the command property however the events as well as the Command is not being called. I'm going crazy over this issue. – Jan Navarro Apr 26 '16 at 06:05