2017-01-26 16 views
1

以下是我控制視圖中檢查的工具,我想從文本塊中的樹下返回值:UIAutomation不會返回兒童元素的值

Control in Inspect tool

在我的WPF應用程序的窗口,一個數據網格有行和列,並試圖獲取特定行和列的文本值。第一行將文本值(timestamp)作爲子樹下的文本塊。

檢查控件的屬性是:

數據網格 - Datagrid的

日誌 - DataItemControlType

項目 - UIA_CustomControlType(DataGridCell)

自定義 - UIA_CustomControlType(DataGridCell)

文本 - UIA_TextControlTypeID

我可以得到總的行數,但無法獲得文本值。

var gridChilds = grid.LowLevelAutomationObject.FindAll(System.Windows.Automation.TreeScope.Children, Condition.TrueCondition).OfType<AutomationElement>(); 
var rows = gridChilds.Count() ; 

List<AutomationElement> messages = new List<AutomationElement>(); 
      TreeWalker walker = new TreeWalker(new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.DataItem)); 
      AutomationElement ae = grdControl as AutomationElement; 
      AutomationElement row = walker.GetFirstChild(ae); 

       messages.Add(row); 
      row = walker.GetNextSibling(row); 
      string result = Convert.ToString(row); 
      return result ; 

我嘗試了一些解決方案,從類似的問題在線幫助,但沒有工作了我的例子。我是C#編程和工具的新手,所以任何幫助將不勝感激。

回答

0

從查看屏幕截圖看,它看起來像你想要的單元格的名稱屬性。所以代碼看起來像這樣。我從你的網格控件開始,並且我還假定這些行的類名是「Record」,因爲網格中的大多數行將具有類名「Record」。

AutomationElement dataGrid = grdControl as AutomationElement; 
AutomationElement outerRow = dataGrid.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "Record")); 
AutomationElement innerRow = outerRow.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "Record")); 
AutomationElement custom = innerRow.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "custom")); 
AutomationElement text = custom.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "text")); 

return text.Name; 
+0

Max Young,非常感謝,我能夠得到一個類似於您提供的上述解決方案的工作。一些「很高興有」是在自定義級別循環並基於我給出的行號,它應該得到文本值。但是,非常感謝您的時間:) – cherryS

+0

如果您需要UI Automation的任何幫助,請隨時給我發消息,沒問題。用可用資源學習UI自動化是非常可怕的。 –

+0

當然,非常感謝您提供幫助。 – cherryS