Responding to context menu.

Scottintexas

Well-known member
Joined
Jun 21, 2018
Messages
47
Location
Texas
Programming Experience
5-10
I have a context menu in a view that calls the correct handler, but the command parameter is not being passed.
View:
 <TextBlock DockPanel.Dock="Left"
                   Margin="0,3,1,0"
                   VerticalAlignment="Center">
            <TextBlock.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Associate USB"
                              Command="{Binding AssociateComportCommand}" />
                    <MenuItem Header= "TestPort"
                              Command="{Binding TestPortCommand}"
                              CommandParameter="{Binding ElementName=PortName, Path=Text}"/>
                    </ContextMenu>
            </TextBlock.ContextMenu>
            <Run Text="Com Port:"
                      Style="{StaticResource TitleStyle}"/>
            <Run  x:Name="PortName"
                      Text="{Binding Comport}"
                      Style="{StaticResource TitleStyle}"/>
            </TextBlock>

ViewModel:
/// <summary>
        /// The event handler for the test port menu item
        /// </summary>
        public event EventHandler TestPort;

        /// <summary>
        /// Handles the Test Port command and fires the event so it can be
        /// picked up in the MainWindowViewModel.
        /// </summary>
        private void OnTestPort(string param)
        {
            var handler = TestPort;                                           //Break point here
            if (handler != null)
                handler(this, new ComPortEventArgs(param));
        }

        private RelayCommand testPortCommand;

        /// <summary>
        /// Allows the user to test the serial port.
        /// </summary>
        public ICommand TestPortCommand
        {
            get
            {
                if (testPortCommand == null)
                {
                    testPortCommand = new RelayCommand(param => OnTestPort((string)param));
                }
                return testPortCommand;
            }
        }

And finally the MainViewModel should subscribe to the event and call the proper method.

C#:
private void OnTestPort(object sender, ComPortEventArgs e)
        {
            ComPortViewModel cpvm = Ports.Where(x=> x.PortName == e.IDString).FirstOrDefault();
            cpvm.TestSerialPort();
        }

A breakpoint in the OnTestPort in the ViewModel with the context menu shows no value for the string param. So I am not getting it from the CommandParameter like I think I should. Also, this never makes it to the MainViewModel. I suppose it never makes it to the other view model because the parameter is missing. Maybe not. The thing is, I have done this before, but that was from a Button. And, yes, the value for the PortName is present.

Any ideas?
 
What happens if you just hard code a value for the the command parameter first in the XAML to make sure that it's not the binding that is causing the issue? Is that hardcoded value being passed in?
 
Thanks, Skydiver,

Hard coding passed the parameter as expected, so I looked at the binding to the parameter. As soon as I try to bind to the text in the run where the port name is, I get null at the command. So it is the binding that is not right, but I don't see anything wrong with it. I am trying to find out if a binding to a run is allowed.
 
Back
Top Bottom