/// --------------------------------------------------------------------
/// <summary>
/// Gets the enclosing element of the target selection.
/// </summary>
/// <param name="sender">The object that raised the event.</param>
/// <param name="e">Event arguments.</param>
/// --------------------------------------------------------------------
private void GetEnclosingElement_Click(object sender, RoutedEventArgs e)
{
// Obtain the enclosing element.
AutomationElement enclosingElement;
try
{
enclosingElement = _searchRange.GetEnclosingElement();
}
catch (ElementNotAvailableException)
{
// TODO: error handling.
return;
}
// Assemble the information about the enclosing element.
var enclosingElementInformation = new StringBuilder();
enclosingElementInformation.Append(
"Enclosing element:\t").AppendLine(
enclosingElement.Current.ControlType.ProgrammaticName);
// The WPF target doesn't show selected text as highlighted unless
// the window has focus.
_targetWindow.SetFocus();
// Display the enclosing element information in the client.
_targetSelectionDetails.Text = enclosingElementInformation.ToString();
// Is the enclosing element the entire document?
// If so, select the document.
if (enclosingElement == _targetDocument)
{
_documentRange = _targetTextPattern.DocumentRange;
_documentRange.Select();
return;
}
// Otherwise, select the range from the child element.
var childRange =
_documentRange.TextPattern.RangeFromChild(enclosingElement);
childRange.Select();
}