none
How to set AutomationID and Name for Popup

    Question

  • I have a code as below
    <Window x:Class="WpfApplication2.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:src="clr-namespace:WpfApplication2"
            AutomationProperties.AutomationId="win1"
        Title="Window1" originalAttribute="src" originalPath="clr-namespace:WpfApplication2"
            AutomationProperties.AutomationId="win1"
        Title="Window1" originalAttribute="src" originalPath="clr-namespace:WpfApplication2"
            AutomationProperties.AutomationId="win1"
        Title="Window1" Height="300" Width="300">
        <StackPanel>
            <src:MyPopUp IsOpen="True" x:Name="pop1" AutomationProperties.AutomationId="popautomationid">
                    <Button>222</Button>
            </src:MyPopUp>
        </StackPanel>
    </Window>


    When I checked it in UIspy, I found that there is no automationid or name or etc.
    The information from UISpy is as below:

      Identification
        ClassName: "Popup"
        ControlType: "ControlType.Window"
        Culture: "(null)"
        AutomationId: ""
        LocalizedControlType: "window"
        Name: ""
        ProcessId: "5076 (WpfApplication2.vshost)"
        RuntimeId: "42 3349124"
        IsPassword: "False"
        IsControlElement: "True"
        IsContentElement: "True"

    Is there any idea about this issue?

    Appreciate for your reply!

    Wednesday, July 16, 2008 7:22 AM

Answers

  • Interesting, it seems that Popup themselves doesn't have any automation peer associated with it, so that it will not be part of the automation tree, what actually appears in the automation tree is the internal PopupRoot, so in order to make the above code working for you, you could try the following code:

    <Window x:Class="WpfTestHarness.SetAutomationIdForPopupDemo"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cc="clr-namespace:WpfTestHarness"
        AutomationProperties.AutomationId="WindowAutomationId"
        Title="SetAutomationIdForPopupDemo" Height="300" Width="300">
      <StackPanel>
        <cc:MyPopup
            IsOpen="True"
            AllowsTransparency="True"
            x:Name="popup"
            AutomationProperties.AutomationId="PopupAutomationId">
          <Border Background="Red">
            <Button Margin="50" x:Name="button">222</Button>
          </Border>
        </cc:MyPopup>
      </StackPanel>
    </Window>

    public class MyPopup : Popup
    {
        static MyPopup()
        {
            AutomationProperties.AutomationIdProperty.OverrideMetadata(typeof(MyPopup),
                new FrameworkPropertyMetadata((DependencyObject sender, DependencyPropertyChangedEventArgs e) =>
                {
                    MyPopup popup = (MyPopup)sender;
                    if (popup.IsOpen)
                    {
                        popup.SetAutomationIdToLogicalChild();
                    }
                    else
                    {
                        popup.Opened += popup.SetAutomationIdToLogicalChildOnLoad;
                    }
                }));
        }

        private void SetAutomationIdToLogicalChildOnLoad(Object sender, EventArgs e)
        {
            SetAutomationIdToLogicalChild();
        }

        private void SetAutomationIdToLogicalChild()
        {
            IEnumerator enumerator = LogicalTreeHelper.GetChildren(this).GetEnumerator();
            if (enumerator != null && enumerator.MoveNext())
            {
                FrameworkElement child = enumerator.Current as FrameworkElement;
                Visual popupRoot = GetVisualParentRoot(child);
                if (popupRoot != null)
                {
                    popupRoot.SetValue(AutomationProperties.AutomationIdProperty,
                        GetValue(AutomationProperties.AutomationIdProperty));
                }
            }
        }

        private Visual GetVisualParentRoot(Visual child)
        {
            Visual parent = child;
            while (parent != null)
            {
                Visual temp = VisualTreeHelper.GetParent(parent) as Visual;
                if (temp == null)
                {
                    return parent;
                }
                else
                {
                    parent = temp;
                }
            }

            return parent;
        }
    }

    Hope this helps
    • Marked as answer by Marco Zhou Tuesday, July 22, 2008 10:40 AM
    Friday, July 18, 2008 7:23 AM

All replies

  • Can anyone give me some suggestion?
    Thursday, July 17, 2008 4:42 AM
  • Interesting, it seems that Popup themselves doesn't have any automation peer associated with it, so that it will not be part of the automation tree, what actually appears in the automation tree is the internal PopupRoot, so in order to make the above code working for you, you could try the following code:

    <Window x:Class="WpfTestHarness.SetAutomationIdForPopupDemo"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cc="clr-namespace:WpfTestHarness"
        AutomationProperties.AutomationId="WindowAutomationId"
        Title="SetAutomationIdForPopupDemo" Height="300" Width="300">
      <StackPanel>
        <cc:MyPopup
            IsOpen="True"
            AllowsTransparency="True"
            x:Name="popup"
            AutomationProperties.AutomationId="PopupAutomationId">
          <Border Background="Red">
            <Button Margin="50" x:Name="button">222</Button>
          </Border>
        </cc:MyPopup>
      </StackPanel>
    </Window>

    public class MyPopup : Popup
    {
        static MyPopup()
        {
            AutomationProperties.AutomationIdProperty.OverrideMetadata(typeof(MyPopup),
                new FrameworkPropertyMetadata((DependencyObject sender, DependencyPropertyChangedEventArgs e) =>
                {
                    MyPopup popup = (MyPopup)sender;
                    if (popup.IsOpen)
                    {
                        popup.SetAutomationIdToLogicalChild();
                    }
                    else
                    {
                        popup.Opened += popup.SetAutomationIdToLogicalChildOnLoad;
                    }
                }));
        }

        private void SetAutomationIdToLogicalChildOnLoad(Object sender, EventArgs e)
        {
            SetAutomationIdToLogicalChild();
        }

        private void SetAutomationIdToLogicalChild()
        {
            IEnumerator enumerator = LogicalTreeHelper.GetChildren(this).GetEnumerator();
            if (enumerator != null && enumerator.MoveNext())
            {
                FrameworkElement child = enumerator.Current as FrameworkElement;
                Visual popupRoot = GetVisualParentRoot(child);
                if (popupRoot != null)
                {
                    popupRoot.SetValue(AutomationProperties.AutomationIdProperty,
                        GetValue(AutomationProperties.AutomationIdProperty));
                }
            }
        }

        private Visual GetVisualParentRoot(Visual child)
        {
            Visual parent = child;
            while (parent != null)
            {
                Visual temp = VisualTreeHelper.GetParent(parent) as Visual;
                if (temp == null)
                {
                    return parent;
                }
                else
                {
                    parent = temp;
                }
            }

            return parent;
        }
    }

    Hope this helps
    • Marked as answer by Marco Zhou Tuesday, July 22, 2008 10:40 AM
    Friday, July 18, 2008 7:23 AM
  • Hello,

    I have a similar problem, I using ItemsControl. For these also AutomationID is empty. How do I set AutomationId please guide me. I am new to Automation..

     

    Tuesday, August 03, 2010 1:45 AM