支持TogglePattern的控件有CheckBox,TreeView中的button控件等。
1. TogglePattern的方法
Toggle方法用于操作可以循环通过的一组状态并在设置后保持某种状态。
2. TogglePattern属性
Current属性中的ToggleState有如下三种状态:
1. On
2. Off
3. Indeterminate
如下代码演示了使用TogglePattern来操作CheckBox控件。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.Text;
3
using System.Diagnostics;
4
using System.Threading;
5
using System.Windows.Automation;
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
namespace UIATest
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
9
class Program
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
11
static void Main(string[] args)
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
Process process = Process.Start(@"F:\CSharpDotNet\AutomationTest\ATP\WpfApp\bin\Debug\WpfApp.exe");
14
int processId = process.Id;
15![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
Thread.Sleep(1000);
17
AutomationElement element = FindElementById(processId, "checkBox1");
18
TogglePattern togglePattern = GetTogglePattern(element);
19
togglePattern.Toggle();
20
}
21![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
23
/// Get the automation elemention of current form.
24
/// </summary>
25
/// <param name="processId">Process Id</param>
26
/// <returns>Target element</returns>
27
public static AutomationElement FindWindowByProcessId(int processId)
28![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
29
AutomationElement targetWindow = null;
30
int count = 0;
31
try
32![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
33
Process p = Process.GetProcessById(processId);
34
targetWindow = AutomationElement.FromHandle(p.MainWindowHandle);
35
return targetWindow;
36
}
37
catch (Exception ex)
38![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
39
count++;
40
StringBuilder sb = new StringBuilder();
41
string message = sb.AppendLine(string.Format("Target window is not existing.try #{0}", count)).ToString();
42
if (count > 5)
43![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
44
throw new InvalidProgramException(message, ex);
45
}
46
else
47![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
48
return FindWindowByProcessId(processId);
49
}
50
}
51
}
52![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
54
/// Get the automation element by automation Id.
55
/// </summary>
56
/// <param name="windowName">Window name</param>
57
/// <param name="automationId">Control automation Id</param>
58
/// <returns>Automatin element searched by automation Id</returns>
59
public static AutomationElement FindElementById(int processId, string automationId)
60![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
61
AutomationElement aeForm = FindWindowByProcessId(processId);
62
AutomationElement tarFindElement = aeForm.FindFirst(TreeScope.Descendants,
63
new PropertyCondition(AutomationElement.AutomationIdProperty, automationId));
64
return tarFindElement;
65
}
66![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
TogglePattern helper#region TogglePattern helper
68![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <param name="element">AutomationElement instance</param>
70
/// <returns>TogglePattern instance</returns>
71
public static TogglePattern GetTogglePattern(AutomationElement element)
72![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
73
object currentPattern;
74
if (!element.TryGetCurrentPattern(TogglePattern.Pattern, out currentPattern))
75![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
76
throw new Exception(string.Format("Element with AutomationId '{0}' and Name '{1}' does not support the TogglePattern.",
77
element.Current.AutomationId, element.Current.Name));
78
}
79
return currentPattern as TogglePattern;
80
}
81![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
82
#endregion
83
}
84
}
85![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
如下代码为对应的XAML:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
<Window x:Class="WpfApp.Window1"
2
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4
Title="Window1" Height="219" Width="353">
5
<Grid>
6
<CheckBox HorizontalAlignment="Right" Margin="0,75,10,89" Name="checkBox1" Width="120">CheckBox</CheckBox>
7
</Grid>
8
</Window>
9![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
本文主要简单介绍了TogglePattern以及使用TogglePattern操作CheckBox的方法。