WPF将FlowDocumentScrollViewer中的当前选择获取到CommandParameter中

时间:2013-04-04 17:58:51

标签: c# wpf mvvm contextmenu commandparameter

尝试将流查看器中的选定文本作为参数

添加到命令中
 <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>

在模型类中:

 private RelayCommand<System.Windows.Documents.TextSelection> _AddDefaultTriggerCommand;

 public ICommand AddDefaultTriggerCommand
 {
     get
     {
         ...
         this._AddDefaultTriggerCommand = new RelayCommand<TextSelection>(
                     AddDefaultTriggerCommandExecuted,...)
         ...
     }
 }

问题在于传递给处理程序的参数是始终为null

 private void AddDefaultTriggerCommandExecuted(System.Windows.Documents.TextSelection parameter)...

我错过了什么吗?标准的Windows复制命令如何获取所选文本?

1 个答案:

答案 0 :(得分:1)

是的,因为你没有传递参数。添加一个lambda表达式,它应该工作:

this._AddDefaultTriggerCommand = new RelayCommand<TextSelection>(
                 param => AddDefaultTriggerCommandExecuted(param))