Scottintexas
Well-known member
I have a context menu in a view that calls the correct handler, but the command parameter is not being passed.
And finally the MainViewModel should subscribe to the event and call the proper method.
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?
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?