如何判断元素是否与Microsoft UI Automation中的PropertyCondition匹配?

Lun*_*ore 5 c# wpf ui-automation

我正在尝试在GridView的特定行中找到一个AutomationElement(因此有许多相同的元素).我正在迭代行中的元素,我想使用匹配器来查看特定元素是否与我传递给它的条件匹配.我从简单的PropertyConditions开始.

这是我的测试:

[TestFixture]
public class ConditionMatcherBehaviour
{
    [Test]
    public void ShouldMatchAPropertyConditionByItsValue()
    {
        var conditionMatcher = new ConditionMatcher();
        var condition = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Pane);
        Assert.True(conditionMatcher.Matches(AutomationElement.RootElement, condition));
    }
}
Run Code Online (Sandbox Code Playgroud)

这是代码:

public class ConditionMatcher : IMatchConditions
{
    public bool Matches(AutomationElement element, Condition condition)
    {
        var propertyCondition = (PropertyCondition) condition;
        return propertyCondition.Value.Equals(element.GetCurrentPropertyValue(propertyCondition.Property));
    }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是测试失败了.根元素(桌面)的ControlType确实是ControlType.Pane,但奇怪的是PropertyCondition.Value是"50033".

关于如何在FindFirst/FindAll之外测试PropertyCondition的任何想法?

(我的解决方法是创建我自己的条件类型并测试相反,但我想检查我是不是误解了某些东西/做了一些愚蠢的事情.)

Lun*_*ore 5

找到了。

public class ConditionMatcher : IMatchConditions
{
    public bool Matches(AutomationElement element, Condition condition)
    {
        return new TreeWalker(condition).Normalize(element) != null;
    }
}
Run Code Online (Sandbox Code Playgroud)

不是很明显,但它适用于匹配和非匹配条件。感谢所有看过并考虑过它的人。希望这会帮助别人!