Wpf 使用UIAutomation从AvalonEdit获取文本

Wpf 使用UIAutomation从AvalonEdit获取文本,wpf,ui-automation,avalonedit,Wpf,Ui Automation,Avalonedit,我正在使用UIAutomation做一些工作,需要在WPF中获取AvaloneEdit控件的内容。我只能将AvaloneEdit控件作为文本的控件类型: var editors = app.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Text)); 这是不受支持的 var targetTextPattern = editor[0].

我正在使用UIAutomation做一些工作,需要在WPF中获取AvaloneEdit控件的内容。我只能将AvaloneEdit控件作为文本的控件类型:

var editors = app.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Text));
这是不受支持的

var targetTextPattern = editor[0].GetCurrentPattern( TextPattern.Pattern) as TextPattern;
我似乎找不到从中提取文本内容的方法,在使用ControlType.text时是否不可能这样做?我也尝试过使用ControlType编辑和文档,但AvaloneEdit似乎不支持它们

欢迎任何帮助。
谢谢

在深入研究源代码之后,我发现AvalonEdit.TextEditor确实支持UIAutomation。这些是使用它所必需的完整步骤

首先,使用ControlType.Custom查找文本编辑器:

allEditors = app.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom));
TextEditorAutomationPeer类实现了IValueProvider,因此要使用UIAutomation从TextEditor获取文本,请使用如下ValuePattern:

var editorValuePattern = allEditors[0].GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
var text = editorValuePattern.Current.Value;
这对我有用:)