WPF在FlowDocumentScrollViewer中获得当前选择到CommandParameter
本文关键字:选择 CommandParameter FlowDocumentScrollViewer WPF | 更新日期: 2023-09-27 18:07:30
尝试将流查看器中的选定文本作为命令的参数
<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>
在model类中:
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复制命令如何获得选定的文本?
是的,因为您没有传递参数。添加一个lambda表达式,它应该可以工作:
this._AddDefaultTriggerCommand = new RelayCommand<TextSelection>(
param => AddDefaultTriggerCommandExecuted(param))