2011-07-17 45 views
1

我正在做一些UIAutomation的工作,需要獲取WPF中的AvalonEdit控件的內容。我只能夠獲得AvalonEdit控制的持有作爲文本的ControlType:使用UIAutomation從AvalonEdit獲取文本

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

這是不支持...

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

我似乎無法找到一個方法來提取文本內容,使用ControlType.Text時無法做到這一點?我也嘗試使用ControlType編輯&文檔,但AvalonEdit似乎不支持它們。

任何幫助是appretiated。 謝謝!

回答

2

經過對源代碼的一些挖掘,我發現AvalonEdit.TextEditor確實支持UIAutomation。這些是使用它所必需的完整步驟。

首先,使用ControlType.Custom找到文本編輯:

allEditors = app.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom)); 

TextEditorAutomationPeer類實現IValueProvider,因此要獲得使用UIAutomation從文本編輯的文本,使用ValuePattern這樣的:

var editorValuePattern = allEditors[0].GetCurrentPattern(ValuePattern.Pattern) as ValuePattern; 
var text = editorValuePattern.Current.Value; 

這爲我工作:)