Split ButtonMenu ItemAutomation
I got a couple of queries from the readers asking if I could provide a slightly non-trivial sample of using automation. So I created a split button user control which looks something like this
If there are no items then you get a checkable button.
The items are created in a popup and are generated on demand. This makes the automation a bit interesting.
As you can see the control supports 3 patterns –
· Invoke (for the button)
· ExpandCollapse (for the drop down)
· Toggle (for the checkable option) – This is just for demo purposes
You will need to specify this in the GetPattern function which overrides the one in the base AutomationPeer.
The other function that you might want to override is GetChildrenCore in case of popups. The default behavior is to go through the visual tree and get the children. However, popups are in a separate visual tree. The sample overrides the function since the items are displayed as part of a popup J
Also for each of the patterns, it is necessary that we implement the provided interfaces. Got this nice little chart from the following link
Control Pattern |
Client-Side Class |
Provider-Side Interfaces |
Dock |
DockPattern |
IDockProvider |
ExpandCollapse |
ExpandCollapsePattern |
IExpandCollapseProvider |
Grid |
GridPattern |
IGridProvider |
GridItem |
GridItemPattern |
IGridItemProvider |
Invoke |
InvokePattern |
IInvokeProvider |
MultipleView |
MultipleViewPattern |
IMultipleViewProvider |
RangeValue |
RangeValuePattern |
IRangeValueProvider |
Scroll |
ScrollPattern |
IScrollProvider |
ScrollItem |
ScrollItemPattern |
IScrollItemProvider |
Selection |
SelectionPattern |
ISelectionProvider |
SelectionItem |
SelectionItemPattern |
ISelectionItemProvider |
Table |
TablePattern |
ITableProvider |
TableItem |
TableItemPattern |
ITableItemProvider |
Text |
TextPattern |
ITextProvider |
Toggle |
TogglePattern |
IToggleProvider |
Transform |
TransformPattern |
ITransformProvider |
Value |
ValuePattern |
IValueProvider |
Window |
WindowPattern |
IWindowProvider |
There are other functions that can be overridden if there is a need to customize the return values. As an example you could override the AccesKeyCore function to append a “Alt” string to the return value.
btw to make the automationwork right we need to create the automationPeer in the overridden OnCreateAutomationPeer call in the custom control.
The sample also shows how the automation can be used for testing purposes. A note to be kept in mind is that when accessing the automationelements, we have to be on a separate thread. Most of the times, the testing process will be separate from the product being tested and hence, will be on a separate thread. However, it might be necessary to place in tests in the code itself. Then this note comes in handy.
The control in the sample could be used as a splitbutton or a menu if needed. The toggle behavior is really not necessary but was thrown in to give the behavior some complexity and to show the usage of more patternInterfaces. :).
The sample code is attached
Comments
Anonymous
March 25, 2008
You've been kicked (a good thing) - Trackback from DotNetKicks.comAnonymous
March 25, 2008
great article... Thanks for this :)