【问题标题】:How can I access the underlying Element of an existing AutomationPeer?如何访问现有 AutomationPeer 的基础元素?
【发布时间】:2014-07-13 05:21:51
【问题描述】:

我最近开始学习/使用 AutomationPeer 类以及如何覆盖其功能。我的一个问题是,是否有办法提取在 AutomationPeer 初始化期间传入的 UIElement。

一个例子:

public class MyRadMenuItemAutomationPeer : RadMenuItemAutomationPeer
{
    public MyRadMenuItemAutomationPeer(RadMenuItem owner)
      : base(owner)
    {

    }

    protected override List<AutomationPeer> GetChildrenCore()
    {
        //originalPeers at this point is a collection of RadMenuItemAutomationPeer
        var originalPeers = base.GetChildrenCore();
        
        //Id like to take each one of these Peers, and somehow cast them or 
        // create a new collection<MyRadMenuItemAutomationPeer> from the root
        // element in the original peer. I know there is the Owner property but
        // that is protected and not visible from the outside.

        var newPeers = 
             //What should be implemented here? An idea I had is something like:
             // originalPeers.Select(p => new MyRadMenuItemAutomationPeer(p.Element))
            //     .ToList(); where p.Element is the way to get the element

        //return newPeers Collection
        return newPeers;
    }

    protected override string GetNameCore()
    {
        //Logic to determing the name Property
        return nameValue;
    }
}

【问题讨论】:

  • 许多从 AutomationPeer 派生的类都有一个 Owner 属性,通常对应于相关的“对象”。对于 UIElementAutomationPeer,所有者是相关的 UIElement。因此,您可以尝试将任何 AutomationPeer 转换为给定的派生类,并检查它。这就是你要找的东西吗?
  • 实际上效果很好!对于实际实现,我将其转换为 FrameworkElementAutomationPeer,但概念仍然相同。谢谢!

标签: c# automation ui-automation microsoft-ui-automation


【解决方案1】:

上面Simon Mourier 提出的解决方案效果很好。他将其作为子评论留下,所以我无法将其标记为答案。

许多从 AutomationPeer 派生的类都有一个 Owner 属性,通常对应于相关的“对象”。对于 UIElementAutomationPeer,所有者是相关的 UIElement。因此,您可以尝试将任何 AutomationPeer 转换为给定的派生类,并检查它。这就是你要找的东西吗?

【讨论】:

    猜你喜欢
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    相关资源
    最近更新 更多