如下代码演示了使用SelectionItemPattern来实现listview item 的多选操作:
![](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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
MutlSelect(new int[]
{ 0, 1 }, processId, false);
18
}
19![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
21
/// Get the automation elemention of current form.
22
/// </summary>
23
/// <param name="processId">Process Id</param>
24
/// <returns>Target element</returns>
25
public static AutomationElement FindWindowByProcessId(int processId)
26![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
27
AutomationElement targetWindow = null;
28
int count = 0;
29
try
30![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
31
Process p = Process.GetProcessById(processId);
32
targetWindow = AutomationElement.FromHandle(p.MainWindowHandle);
33
return targetWindow;
34
}
35
catch (Exception ex)
36![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
37
count++;
38
StringBuilder sb = new StringBuilder();
39
string message = sb.AppendLine(string.Format("Target window is not existing.try #{0}", count)).ToString();
40
if (count > 5)
41![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
42
throw new InvalidProgramException(message, ex);
43
}
44
else
45![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
46
return FindWindowByProcessId(processId);
47
}
48
}
49
}
50![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
53
/// Get the automation element by automation Id.
54
/// </summary>
55
/// <param name="windowName">Window name</param>
56
/// <param name="automationId">Control automation Id</param>
57
/// <returns>Automatin element searched by automation Id</returns>
58
public static AutomationElement FindElementById(int processId, string automationId)
59![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
60
AutomationElement aeForm = FindWindowByProcessId(processId);
61
AutomationElement tarFindElement = aeForm.FindFirst(TreeScope.Descendants,
62
new PropertyCondition(AutomationElement.AutomationIdProperty, automationId));
63
return tarFindElement;
64
}
65
66![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
67
/// Bulk select the list item
68
/// </summary>
69
/// <param name="indexes">List item index collection</param>
70
/// <param name="processId">Application process Id</param>
71
/// <param name="isSelectAll">Is select all or not</param>
72
public static void MutlSelect(int[] indexes, int processId, bool isSelectAll)
73![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
74
AutomationElement targetElement = FindElementById(processId, "listView1");
75![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
76
AutomationElementCollection rows =
77
targetElement.FindAll(TreeScope.Descendants,
78
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ListItem));
79![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
80
object multiSelect;
81![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
82
if (isSelectAll)
83![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
84
for (int i = 1; i < rows.Count - 1; i++)
85![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
86
if (rows[i].TryGetCurrentPattern(SelectionItemPattern.Pattern, out multiSelect))
87![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
88
(multiSelect as SelectionItemPattern).AddToSelection();
89
}
90
}
91
}
92
else
93![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
94
if (indexes.Length > 0)
95![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
96
for (int j = 0; j < indexes.Length; j++)
97![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
98
int tempIndex = indexes[j];
99
if (rows[tempIndex].TryGetCurrentPattern(SelectionItemPattern.Pattern, out multiSelect))
100![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
101
(multiSelect as SelectionItemPattern).AddToSelection();
102
}
103
}
104
}
105
}
106
}
107![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
108![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
SelectItemPattern#region SelectItemPattern
109![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
110![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
111
/// Get SelectItemPattern
112
/// </summary>
113
/// <param name="element">AutomationElement instance</param>
114
/// <returns>SelectItemPattern instance</returns>
115
public static SelectionItemPattern GetSelectionItemPattern(AutomationElement element)
116![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
117
object currentPattern;
118
if (!element.TryGetCurrentPattern(SelectionItemPattern.Pattern, out currentPattern))
119![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
120
throw new Exception(string.Format("Element with AutomationId '{0}' and Name '{1}' does not support the SelectionItemPattern.",
121
element.Current.AutomationId, element.Current.Name));
122
}
123
return currentPattern as SelectionItemPattern;
124
}
125![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
126
#endregion
127
}
128
} 如下代码为对应的XAML:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
<Window x:Class="WpfApp.Window2"
2
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4
Title="Window2" Height="412" Width="585">
5
<Grid>
6
<ListView Margin="2,97,0,163" Name="listView1">
7
<ListViewItem>Kaden</ListViewItem>
8
<ListViewItem>KangYi</ListViewItem>
9
<ListViewItem>John</ListViewItem>
10
</ListView>
11
</Grid>
12
</Window>
13![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)