0

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?

Califf
  • 504
  • 6
  • 17

1 Answers1

1

Yes, because you didn't pass the parameter. Add a lambda expression and it should work:

this._AddDefaultTriggerCommand = new RelayCommand<TextSelection>(
                 param => AddDefaultTriggerCommandExecuted(param))
stratever
  • 586
  • 4
  • 11
  • one more thing I came accross accidentally is that I had to add this line into Window ctor to get the context right: // Required in order to use the ElementName Binding in a context menu NameScope.SetNameScope(MainWndContextMenu, NameScope.GetNameScope(this)); combined, it worked perfectly, thanks! – Califf Apr 04 '13 at 18:44