代码之家  ›  专栏  ›  技术社区  ›  Orion Edwards

WPF UI自动化-不支持行(或者如何选择和取消选择整行)

  •  2
  • Orion Edwards  · 技术社区  · 16 年前


    我有一个使用DataGrid的WPF应用程序;我正在使用WPF ui自动化API为其编写一些自动化测试。

    我所处的位置是,从我的测试中可以找到datagrid,并且我可以使用 GridPattern.GetItem 方法,并通过设置 SelectionItemPattern.Select . 方法

    代码看起来有点像这样:

    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();
    

    这似乎是可行的,但它只选择第一个单独的单元格,而不是整个表行(有10列),但我不知道如何选择 取消选择 SelectionItemPattern.AddToSelection() itemPattern ,或相应的 RemoveFromSelection

    => Cannot change cell selection when the SelectionUnit is FullRow.
       at MS.Internal.Automation.ElementUtil.Invoke(AutomationPeer peer, DispatcherOperationCallback work, Object arg)
       at MS.Internal.Automation.SelectionItemProviderWrapper.AddToSelection()
    

    潜在的根问题似乎是(就我所见),WPF UI自动化API没有网格行的概念,只有单元格。这似乎有点问题,对吗?

    2 回复  |  直到 6 年前
        1
  •  0
  •   Samuel Jack    16 年前

    如果你使用 UISpy

    我怀疑发生的事情是这样的 var item = mainGridPattern.GetItem(rowToSelect, 0);

    你可以打电话来 item.CachedParent -然后 Select()

        2
  •  0
  •   Orion Edwards    16 年前

    SelectionItemPattern ,因此您现在可以选择/取消选择行。好极了

    推荐文章