實作 UI 自動化 Selection 控制項模式Implementing the UI Automation Selection Control Pattern

注意

這份文件適用於想要使用 UI 自動化UI Automation 命名空間中定義之 Managed System.Windows.Automation 類別的 .NET Framework 開發人員。This documentation is intended for .NET Framework developers who want to use the managed UI 自動化UI Automation classes defined in the System.Windows.Automation namespace. 如需 UI 自動化UI Automation的最新資訊,請參閱 Windows Automation API:UI 自動化For the latest information about UI 自動化UI Automation, see Windows Automation API: UI Automation.

本主題將介紹實作 ISelectionProvider的方針和慣例,包括事件和屬性的相關資訊。This topic introduces guidelines and conventions for implementing ISelectionProvider, including information about events and properties. 其他參考的連結列於主題的結尾。Links to additional references are listed at the end of the topic.

SelectionPattern 控制項模式是用以支援當作可選取子項目集合的容器使用的控制項。The SelectionPattern control pattern is used to support controls that act as containers for a collection of selectable child items. 這個項目的子系必須實作 ISelectionItemProviderThe children of this element must implement ISelectionItemProvider. 如需實作此控制項模式的控制項範例,請參閱 Control Pattern Mapping for UI Automation ClientsFor examples of controls that implement this control pattern, see Control Pattern Mapping for UI Automation Clients.

實作方針和慣例Implementation Guidelines and Conventions

實作選取控制項模式時,請注意下列方針和慣例:When implementing the Selection control pattern, note the following guidelines and conventions:

已反白顯示黃色的色彩選擇器Color picker with yellow highlighted.
色樣字串對應範例Example of Color Swatch String Mapping

  • 功能表不支援 SelectionPatternMenus do not support SelectionPattern. 如果您使用的功能表項目包括圖形和文字 (例如 Microsoft Outlook 的 [ View ] 功能表中的 [預覽] 窗格 專案) 而且需要傳達狀態,則應該執行 IToggleProviderIf you are working with menu items that include both graphics and text (such as the Preview Pane items in the View menu in Microsoft Outlook) and need to convey state, you should implement IToggleProvider.

ISelectionProvider 的必要成員Required Members for ISelectionProvider

ISelectionProvider 介面需要下列屬性、方法和事件。The following properties, methods, and events are required for the ISelectionProvider interface.

必要成員Required members 類型Type 注意Notes
CanSelectMultiple 屬性Property 應支援使用 AddAutomationPropertyChangedEventHandlerRemoveAutomationPropertyChangedEventHandler的屬性變更事件。Should support property changed events using AddAutomationPropertyChangedEventHandler and RemoveAutomationPropertyChangedEventHandler.
IsSelectionRequired 屬性Property 應支援使用 AddAutomationPropertyChangedEventHandlerRemoveAutomationPropertyChangedEventHandler的屬性變更事件。Should support property changed events using AddAutomationPropertyChangedEventHandler and RemoveAutomationPropertyChangedEventHandler.
GetSelection 方法Method None
InvalidatedEvent 事件Event 當容器中的選項大幅變更,需要傳送比 InvalidateLimit 常數所允許的更多新增和移除事件時,會引發此事件。Raised when a selection in a container has changed significantly and requires sending more addition and removal events than the InvalidateLimit constant permits.

IsSelectionRequiredCanSelectMultiple 屬性可以是動態的。The IsSelectionRequired and CanSelectMultiple properties can be dynamic. 例如,控制項的最初狀態可能預設沒有選取任何項目,表示 IsSelectionRequiredfalseFor example, the initial state of a control might not have any items selected by default, indicating that IsSelectionRequired is false. 不過,在選取項目之後,控制項就必須至少有一個項目一律保持為選取。However, after an item is selected, the control must always have at least one item selected. 同樣地,在極少數的情況下,控制項可能會允許最初選取多個項目,但之後就只允許單一選取。Similarly, in rare cases, a control might allow multiple items to be selected on initialization, but subsequently allow only single selections to be made.

例外狀況Exceptions

提供者必須擲回下列例外狀況。Providers must throw the following exceptions.

例外狀況類型Exception Type 條件Condition
ElementNotEnabledException 如果未啟用此控制項。If the control is not enabled.
InvalidOperationException 如果隱藏此控制項。If the control is hidden.

另請參閱See also