0

In WPF, I have this Button (with a ContextMenu) that has a MyString string property defined in the code behind. I'm trying to send this string as the CommandParameter of a DelegateCommand called when one of the MenuItem of the ContextMenu is called.

The DelegateCommand call in the ViewModel works, but the CommandParameter is not being passed along it.

I've tried different approaches for the MenuItem code, such as 1) defining a name for the Button in order to use the ElementName attribute, as well as 2) trying to get it by finding string property by the ancestor type, but neither worked. I get the parameter as null instead in the ViewModel.

Approach 1:

<Button x:Name=MyButtonName"
    <some code supressed...>
    <Button.ContextMenu>
        <ContextMenu>
            <MenuItem x:Name="SomeMenuAction">
                <b:Interaction.Triggers>
                    <b:EventTrigger EventName="Click">
                        <b:InvokeCommandAction
                            Command="{Binding Path=MyDelegateCommand}"
                            CommandParameter="{Binding Name, ElementName=MyButtonName}"/>                                                 
                        </b:EventTrigger>
                    </b:Interaction.Triggers>
             </MenuItem>
         </ContextMenu>
    </Button.ContextMenu>
</Button>

Approach 2:

CommandParameter="{Binding Name, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:WorkspaceButton}}}"

This is the code behind it:

public partial class WorkspaceButton : Button
{
    public string Name { get; set; }

    public WorkspaceButton(string name)
    {
        InitializeComponent();
        if (name == String.Empty) return;
        Name = name;
    }
}

In the ViewModel I have the following code:

DelegateCommand<object> _myDelegateCommand;
public DelegateCommand<object> MyDelegateCommand => _myDelegateCommand
    = new DelegateCommand<object>(ExecuteMyDelegateCommand);

void ExecuteMyDelegateCommand(object parameter)
{
    // do something with the string parameter received
}

For some reason, if I type a string like "This is a test", it works...

 <MenuItem x:Name="PinOrUnpinAction">
    <b:Interaction.Triggers>
        <b:EventTrigger EventName="Click">
            <b:InvokeCommandAction
                Command="{Binding Path=MyDelegateCommand}"
                CommandParameter="This is a test" />                                                 
            </b:EventTrigger>
        </b:Interaction.Triggers>
 </MenuItem>

I'd appreciate if anyone could help out.

roccaforte
  • 81
  • 5
  • 1
    `MenuItem` already has a [`Command`](https://learn.microsoft.com/dotnet/api/system.windows.controls.menuitem.command?view=windowsdesktop-7.0) and also a `CommandParameter` property which are used when you click – Sir Rufo May 13 '23 at 19:12
  • 1
    The context menu is not in the same visual tree as the button is in, so this binding won't work. The question flagged as duplicate contains several answers on how to work around this. – Lennart May 14 '23 at 12:33

0 Answers0