0
votes

We have a user control with context menu on one of its child controls.
The Command is bound to a RelayCommand in the ViewModel.
However, the command has to act on another child control in the View.
What is the best way to do this? I have tried passing the desired child control as a parameter, but I think the syntax is incorrect:

                        <Controls:ContextMenu >
                        <Controls:MenuItem Header="Center" >
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="Click" >
                                    <GalaSoft_MvvmLight_Command:EventToCommand 
                                        Command="{Binding RecenterCommand}"
                                        CommandParameter="{Binding ElementName=scrollViewer}" />
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                        </Controls:MenuItem>
                    </Controls:ContextMenu>

The command:

RecenterCommand = new RelayCommand<ScrollViewer>(Recenter);  
private void Recenter(ScrollViewer obj)  
{   
}    

When I use the context menu, Recenter() is called, but the obj param is null.
What is the correct syntax for the ElementName attribute in the CommandParameter binding?

UPDATE: I tried changing the CommandParameter to:

CommandParameter="{Binding ElementName=LayoutRoot, Path=scrollViewer}"

...but still doesn't work.

Thanks for any insights....

1
is your Element actually x:Name=scrollViewer? - ecathell
Yes, it is: <ScrollViewer x:Name="scrollViewer" - Number8

1 Answers

0
votes

I think that you need to add PassEventArgsToCommand="True" so, the code will be like this:

<GalaSoft_MvvmLight_Command:EventToCommand  PassEventArgsToCommand="True":
                                    Command="{Binding RecenterCommand}" 
                                    CommandParameter="{Binding ElementName=scrollViewer}" />

and if you want to cath the data in codebehind put (this is code from one of my programs used for a context menu item)

SelectedEmployer e = ((MenuItem)e).DataContext as Employer

I hope this will solve your problem, because you gave me a hint in how to solve one of my problems..