2013-07-30 27 views
0

檢查是否選擇了AutomationElement。我用下面的代碼來獲得AutomationElement對象。如果選擇了AutomationElement,是否有任何方法可用

System.Windows.Point point = new System.Windows.Point(Cursor.Position.X, Cursor.Position.Y); 
AutomationElement element = AutomationElement.FromPoint(point); 
//how to know element is selected or not 

回答

1

如果你的元素包含在ItemContainer控制(即列表框,GridView控件,組合框等),那麼你可以使用元素本身的SelectionItem模式。

SelectionItemPattern selectionItemPattern = element.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern; bool isSelected = selectionItemPattern.Current.IsSelected;

+0

即使它不是一個ItemContainer內部的控制可以實現的選擇模式。爲了安全,請檢查selectionItemPattern的值null。 – Ameen

+0

或者使用安全的TryGetCurrentPattern方法。 –

相關問題