GridPattern.GetItem Method (Int32, Int32)

 

Retrieves an AutomationElement that represents the specified cell.

Namespace:   System.Windows.Automation
Assembly:  UIAutomationClient (in UIAutomationClient.dll)

public AutomationElement GetItem(
	int row,
	int column
)

Parameters

row
Type: System.Int32

The ordinal number of the row of interest.

column
Type: System.Int32

The ordinal number of the column of interest.

Return Value

Type: System.Windows.Automation.AutomationElement

An AutomationElement that represents the retrieved cell.

Exception Condition
ArgumentOutOfRangeException

Either the requested row or column coordinate is less than zero.

ArgumentOutOfRangeException

The requested row coordinate is larger than the RowCount or the column coordinate is larger than the ColumnCount.

Grid coordinates are zero-based with the upper left (or upper right cell depending on locale) having coordinates (0,0).

If a cell is empty, a UI Automation element is still returned in order to support retrieval of the ContainingGrid property for that cell. This is possible when the layout of child elements in the grid is similar to a ragged array as shown here.

Windows Explorer view showing ragged layout.

Example of a Grid Control with Empty Coordinates

In the following example, an AutomationFocusChangedEvent listener is declared to track the traversal of grid items within a grid container. Item properties are echoed to the console upon each focus change event.

///--------------------------------------------------------------------
/// <summary>
/// Set up grid item event listeners.
/// </summary>
/// <param name="targetControl">
/// The grid item container of interest.
/// </param>
///--------------------------------------------------------------------
private void SetGridItemEventListeners(AutomationElement targetControl)
{
    AutomationFocusChangedEventHandler gridItemFocusChangedListener =
        new AutomationFocusChangedEventHandler(OnGridItemFocusChange);
    Automation.AddAutomationFocusChangedEventHandler(
        gridItemFocusChangedListener);
}
///--------------------------------------------------------------------
/// <summary>
/// Event handler for grid item focus change.
/// Can be used to track traversal of individual grid items 
/// within a grid.
/// </summary>
/// <param name="src">Object that raised the event.</param>
/// <param name="e">Event arguments.</param>
///--------------------------------------------------------------------
private void OnGridItemFocusChange(
    object src, AutomationFocusChangedEventArgs e)
{
    // Make sure the element still exists. Elements such as tooltips
    // can disappear before the event is processed.
    AutomationElement sourceElement;
    try
    {
        sourceElement = src as AutomationElement;
    }
    catch (ElementNotAvailableException)
    {
        return;
    }

    // Gets a GridItemPattern from the source of the event.
    GridItemPattern gridItemPattern = 
        GetGridItemPattern(sourceElement);

    if (gridItemPattern == null)
    {
        return;
    }

    // Gets a GridPattern from the grid container.
    GridPattern gridPattern = 
        GetGridPattern(gridItemPattern.Current.ContainingGrid);

    if (gridPattern == null)
    {
        return;
    }

    AutomationElement gridItem = null;
    try
    {
        gridItem = gridPattern.GetItem(
        gridItemPattern.Current.Row, 
        gridItemPattern.Current.Column);
    }
    catch (ArgumentOutOfRangeException)
    {
        // If the requested row coordinate is larger than the RowCount 
        // or the column coordinate is larger than the ColumnCount.
        // -- OR --
        // If either of the requested row or column coordinates 
        // are less than zero.
        // TO DO: error handling.
    }

    // Further event processing can be done at this point.
    // For the purposes of this sample we just report item properties.
    StringBuilder gridItemReport = new StringBuilder();
    gridItemReport.AppendLine(
        gridItemPattern.Current.Row.ToString()).AppendLine(
        gridItemPattern.Current.Column.ToString()).AppendLine(
        gridItemPattern.Current.RowSpan.ToString()).AppendLine(
        gridItemPattern.Current.ColumnSpan.ToString()).AppendLine(
        gridItem.Current.AutomationId.ToString());
    Console.WriteLine(gridItemReport.ToString());
}

///--------------------------------------------------------------------
/// <summary>
/// Handles our application shutdown.
/// </summary>
/// <param name="args">Event arguments.</param>
///--------------------------------------------------------------------
protected override void OnExit(System.Windows.ExitEventArgs args)
{
    Automation.RemoveAllEventHandlers();
    base.OnExit(args);
}

.NET Framework
Available since 3.0
Return to top
Show: