使用UIAutomation从AvalonEdit获取文本

时间:2011-07-17 08:03:50

标签: wpf ui-automation avalonedit

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

var editors = app.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Text));

这不受支持......

var targetTextPattern = editor[0].GetCurrentPattern( TextPattern.Pattern) as TextPattern;

我似乎找不到从中提取文本内容的方法,使用ControlType.Text时是不是可以做到?我也尝试过使用ControlType Edit&文档,但AvalonEdit似乎不支持它们。

任何帮助都是有意义的。 谢谢!

1 个答案:

答案 0 :(得分:2)

在深入挖掘源代码后,我发现AvalonEdit.TextEditor支持UIAutomation。这些是使用它所需的完整步骤。

首先,使用ControlType.Custom查找TextEditor:

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;

这对我有用:)