Export (0) Print
Expand All

Support Control Patterns in a UI Automation Provider

This topic shows how to implement one or more control patterns on a UI Automation provider so that client applications can manipulate controls and get data from them.

Support Control Patterns

  1. Implement the appropriate interfaces for the control patterns that the element should support, such as IInvokeProvider for InvokePattern.

  2. Return the object containing your implementation of each control interface in your implementation of IRawElementProviderSimple.GetPatternProvider

The following example shows an implementation of ISelectionProvider for a single-selection custom list box. It returns three properties and gets the currently selected item.

#region ISelectionProvider Members

/// <summary> 
/// Specifies whether selection of more than one item at a time is supported. 
/// </summary> 
public bool CanSelectMultiple
{
    get
    {
        return false;
    }
}

/// <summary> 
/// Specifies whether the list has to have an item selected at all times. 
/// </summary> 
public bool IsSelectionRequired
{
    get
    {
        return true;
    }
}

/// <summary> 
/// Returns the automation provider for the selected list item. 
/// </summary> 
/// <returns>The selected item.</returns> 
/// <remarks> 
/// MyList is an ArrayList collection of providers for items in the list box. 
/// SelectedIndex is the index of the selected item. 
/// </remarks> 
public IRawElementProviderSimple[] GetSelection()
{
    if (SelectedIndex >= 0)
    {
        IRawElementProviderSimple itemProvider = (IRawElementProviderSimple)MyList[SelectedIndex];
        IRawElementProviderSimple[] providers =  { itemProvider };
        return providers;
    }
    else return null;
}
#endregion ISelectionProvider Members

The following example shows an implementation of GetPatternProvider that returns the class implementing ISelectionProvider. Most list box controls would support other patterns as well, but in this example a null reference (Nothing in Microsoft Visual Basic .NET) is returned for all other pattern identifiers.

/// <summary> 
/// Returns the object that supports the specified pattern. 
/// </summary> 
/// <param name="patternId">ID of the pattern.</param>
/// <returns>Object that implements IInvokeProvider.</returns> 
/// <remarks> 
/// In this case, the ISelectionProvider interface is implemented in another provider-defined class,  
/// ListPattern. However, it could be implemented in the base provider class, in which case the  
/// method would simply return "this".
/// </remarks> 
object IRawElementProviderSimple.GetPatternProvider(int patternId)
{
    if (patternId == SelectionPatternIdentifiers.Pattern.Id)
    {
        return new ListPattern(myItems, SelectedIndex);
    }
    else
    {
        return null;
    }
}
Show:
© 2014 Microsoft