SelectionItemPattern.RemoveFromSelection Método

Definição

Remove o elemento atual da coleção de itens selecionados.

public void RemoveFromSelection ();

Exceções

É feita uma tentativa de remover uma seleção de um contêiner de seleção em que um item é selecionado e IsSelectionRequiredProperty = true.

Exemplos

O exemplo a seguir mostra como remover um elemento da coleção de itens selecionados no momento.

///--------------------------------------------------------------------
/// <summary>
/// Retrieves the selection container for a selection item.
/// </summary>
/// <param name="selectionItem">
/// An automation element that supports SelectionItemPattern.
/// </param>
///--------------------------------------------------------------------
private AutomationElement GetSelectionItemContainer(
    AutomationElement selectionItem)
{
    // Selection item cannot be null
    if (selectionItem == null)
    {
        throw new ArgumentException();
    }

    SelectionItemPattern selectionItemPattern = 
        selectionItem.GetCurrentPattern(SelectionItemPattern.Pattern) 
        as SelectionItemPattern;
    if (selectionItemPattern == null)
    {
        return null;
    }
    else
    {
        return selectionItemPattern.Current.SelectionContainer; 
    }
}
///--------------------------------------------------------------------
/// <summary>
/// Attempts to remove the current item from a collection 
/// of selected items. 
/// </summary>
/// <param name="selectionItem">
/// An automation element that supports SelectionItemPattern.
/// </param>
///--------------------------------------------------------------------
private void RemoveItemFromSelection(AutomationElement selectionItem)
{
    if (selectionItem == null)
    {
        throw new ArgumentNullException(
            "Argument cannot be null or empty.");
    }

    AutomationElement selectionContainer =
        GetSelectionItemContainer(selectionItem);

    // Selection container cannot be null
    if (selectionContainer == null)
    {
        throw new ElementNotAvailableException();
    }

    SelectionPattern selectionPattern =
        selectionContainer.GetCurrentPattern(SelectionPattern.Pattern)
        as SelectionPattern;

    if (selectionPattern == null)
    {
        return;
    }

    // Check if a selection is required
    if (selectionPattern.Current.IsSelectionRequired && 
        (selectionPattern.Current.GetSelection().GetLength(0) <= 1))
    {
        return;
    }

    SelectionItemPattern selectionItemPattern =
        selectionItem.GetCurrentPattern(
        SelectionItemPattern.Pattern)
        as SelectionItemPattern;
    if ((selectionItemPattern != null) && 
        selectionItemPattern.Current.IsSelected)
    {
        try
        {
            selectionItemPattern.RemoveFromSelection();
        }
        catch (InvalidOperationException)
        {
            // Unable to remove from selection
            return;
        }
    }
}

Aplica-se a

Produto Versões
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9