UI Automation Control Patterns for Clients

Note

This documentation is intended for .NET Framework developers who want to use the managed UI Automation classes defined in the System.Windows.Automation namespace. For the latest information about UI Automation, see Windows Automation API: UI Automation.

This overview introduces control patterns for UI Automation clients. It includes information on how a UI Automation client can use control patterns to access information about the user interface (UI).

Control patterns provide a way to categorize and expose a control's functionality independent of the control type or the appearance of the control. UI Automation clients can examine an AutomationElement to determine which control patterns are supported and be assured of the behavior of the control.

For a complete list of control patterns, see UI Automation Control Patterns Overview.

This topic contains the following sections.

  • Getting Control Patterns
  • Retrieving Properties on Control Patterns
  • Controls with Variable Patterns
  • Related Topics

Getting Control Patterns

Clients retrieve a control pattern from an AutomationElement by calling either AutomationElement.GetCachedPattern or AutomationElement.GetCurrentPattern.

Clients can use the GetSupportedPatterns method or an individual IsPatternAvailable property (for example, IsTextPatternAvailableProperty) to determine if a pattern or group of patterns is supported on the AutomationElement. However, it is more efficient to attempt to get the control pattern and test for a null reference than to check the supported properties and retrieve the control pattern since it results in fewer cross-process calls.

The following example demonstrates how to get a TextPattern control pattern from an AutomationElement.

// Specify the control type we're looking for, in this case 'Document'
PropertyCondition cond = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document);

// target --> The root AutomationElement.
AutomationElement textProvider = target.FindFirst(TreeScope.Descendants, cond);

targetTextPattern = textProvider.GetCurrentPattern(TextPattern.Pattern) as TextPattern;

if (targetTextPattern == null)
{
    Console.WriteLine("Root element does not contain a descendant that supports TextPattern.");
    return;
}

Retrieving Properties on Control Patterns

Clients can retrieve the property values on control patterns by calling either AutomationElement.GetCachedPropertyValue or AutomationElement.GetCurrentPropertyValue and casting the object returned to an appropriate type. For more information on UI Automation properties, see UI Automation Properties for Clients.

In addition to the GetPropertyValue methods, property values can be retrieved through the common language runtime (CLR) accessors to access the UI Automation properties on a pattern.

Controls with Variable Patterns

Some control types support different patterns depending on their state or the manner in which the control is being used. Examples of controls that can have variable patterns are list views (thumbnails, tiles, icons, list, details), Microsoft Excel Charts (Pie, Line, Bar, Cell Value with a formula), Microsoft Word's document area (Normal, Web Layout, Outline, Print Layout, Print Preview), and Microsoft Windows Media Player skins.

Controls implementing custom control types can have any set of control patterns that are needed to represent their functionality.

See Also

Tasks

Invoke a Control Using UI Automation

Get the Toggle State of a Check Box Using UI Automation

Concepts

Control Pattern Mapping for UI Automation Clients

Other Resources

UI Automation Control Patterns

UI Automation Text Pattern

TextPattern Insert Text Sample

TextPattern Search and Selection Sample

InvokePattern and ExpandCollapsePattern Menu Item Sample