WPF ListView : Select ListViewRow and MultiSelect ListViewRow

Oct 25, 2011 at 8:57 AM

I need to find a row which contains specific text in a ListView and select it. Here is the method:

public static bool SetRowSelected(ListView lstView, string criteria) {
            foreach(var row in lstView.Rows) {
                var childs = GetAllChildren(row.AutomationElement, Condition.TrueCondition);
                var child = childs.Where(c => c.Current.Name.Contains(criteria)).FirstOrDefault();
                if(child != null) {
                    return true;
            return false;
public static List<AutomationElement> GetAllChildren(AutomationElement root, Condition condition) {
            TreeWalker walker = new TreeWalker(condition);
            List<AutomationElement> results = new List<AutomationElement>();
            GetAllChildren(root, walker, results);
            return results;

 static void GetAllChildren(AutomationElement current, TreeWalker walker, List<AutomationElement> results) {             results.Add(current);             AutomationElement child = walker.GetFirstChild(current);             while(child != null) {                 GetAllChildren(child, walker, results);                 child = walker.GetNextSibling(child);             }         }

The problem is that sometimes I cannot get the correct rows collection: the method GetAllChildren(AutomationElement root, Condition condition) returns only one AutomationElement whereas
I have many rows in the listview (each row have many cells).

P/S: I tried to use the following statement but it didn't give the correct result:
var childs1 = row.AutomationElement.FindAll(TreeScope.Descendants, Condition.TrueCondition);

Could you please help?
Thanks in advance.
You can contact with me via: chithanh_12@yahoo.com

Oct 26, 2011 at 4:23 AM

You might want to check whether the current row is visible or not. If the row is not visible, use vertical scroll bar to scroll down to make it visible before calling the GetAllChildren method.

Oct 26, 2011 at 4:27 AM

The current row is visible :(