如何访问现有Automation Peer的基础元素?

时间:2014-05-23 18:31:24

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

我最近开始学习/使用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;
        }
    }

任何反馈或建议将不胜感激!

1 个答案:

答案 0 :(得分:0)

Simon Mourier提出的解决方案非常有效。他把它留作子评论,所以我无法将其标记为答案。

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