如何访问现有AutomationPeer的底层元素?

本文关键字:AutomationPeer 元素 何访问 访问 | 更新日期: 2023-09-27 18:04:54

我最近开始学习/使用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的底层元素?

Simon Mourier提出的解决方案非常有效。他把它作为子评论留下了,所以我不能把它标记为回答。

许多从AutomationPeer派生的类都有一个Owner属性,该属性对应于相关的"对象"。一般来说。对于UIElementAutomationPeer, Owner是相关的UIElementAutomationPeer。因此,您可以尝试将任意AutomationPeer强制转换为给定的派生类,并进行检查。这就是你想要的吗?