Trying to get selected text in a flow viewer to a command as a parameter
<FlowDocumentScrollViewer Name="_OutputBox">
<FlowDocument>
<FlowDocument.ContextMenu >
<ContextMenu>
<MenuItem Header="New"
Command="{Binding AddDefaultTriggerCommand}"
CommandParameter="{Binding ElementName=_OutputBox, Path=Selection}">
</MenuItem>
</ContextMenu>
</FlowDocument.ContextMenu>
</FlowDocument>
</FlowDocumentScrollViewer>
In the model class:
private RelayCommand<System.Windows.Documents.TextSelection> _AddDefaultTriggerCommand;
public ICommand AddDefaultTriggerCommand
{
get
{
...
this._AddDefaultTriggerCommand = new RelayCommand<TextSelection>(
AddDefaultTriggerCommandExecuted,...)
...
}
}
The problem is that in the parameter passed into the handler is always null:
private void AddDefaultTriggerCommandExecuted(System.Windows.Documents.TextSelection parameter)...
Am I missing something? How does the standard windows Copy command get the selected text?
Yes, because you didn't pass the parameter. Add a lambda expression and it should work:
this._AddDefaultTriggerCommand = new RelayCommand<TextSelection>(
param => AddDefaultTriggerCommandExecuted(param))