2013-07-10 46 views
1

我在寫一個UI自動化軟件。我需要在數據網格中選擇一行,然後點擊運行按鈕。我在互聯網上嘗試了大部分示例代碼,但它們並不適合我。例如,用於選擇一個gridview行:使用UI自動化選擇一行數據網格

當我寫入以下代碼:

AutomationElement dataGrid = this.mainWindow.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "2885")); 

if (dataGrid != null) 
{ 
    GridPattern pattern = GetGridPattern(dataGrid); 
    AutomationElement tempElement = pattern.GetItem(1, 1); 
    tempElement.SetFocus(); 
} 

我收到錯誤:「目標元件不能接收焦點」這與最後一行有關。

我也試過代碼:

AutomationElement mainGrid = // find the grid in the window 
var columnCount = (int)mainGrid.GetCurrentPropertyValue(GridPattern.ColumnCountProperty); 

var mainGridPattern = (GridPattern)mainGrid.GetCurrentPattern(GridPattern.Pattern); 

var rowToSelect = 2; 

// select just the first cell 
var item = mainGridPattern.GetItem(rowToSelect, 0); 

var itemPattern = (SelectionItemPattern)item.GetCurrentPattern(SelectionItemPattern.Pattern); 

itemPattern.Select(); 

,但我和我收到的錯誤:「不支持的模式」。

我應該提到,我正在使用UI Spy檢索元素屬性。

你能解釋我什麼是錯的,應該如何選擇一行? ! [UI間諜] [1]

回答

2

這裏是你如何能做到這一點:

 // get to ROW X (here it's row #1 name is always "Row X") 
     AutomationElement row1 = dataGrid.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Row 1")); 

     // get row header 
     AutomationElement row1Header = row1.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Header)); 

     // invoke it (select the whole line) 
     ((InvokePattern)row1Header.GetCurrentPattern(InvokePattern.Pattern)).Invoke(); 

要找到這些操作,您可以使用UISpy,並嘗試在樹中不同的項目,看看圖案每個項目都使用UISpy上下文「控制模式」菜單來實現和嘗試它們。

相關問題