none
Automation - textblocks not visible from template

    Question

  • We have an expander template that includes controls and textblocks. The textblocks are completely invisible to UISpy.

    Expander control template is basically:

    <DockPanel>
     <Grid>
     <ToggleButton/>
     <Image/>
     <TextBlock/>
     <TextBlock/>
     <TextBlock/>
     <Image/>
    </Grid>
    </Dockpanel>
    

    This is used in another datatemplate - basically another grid, the expander above, some borders around an itemscontrol. Everything has an x:Name. The expander is in a resource dictionary.

    How do I get these textblocks to appear?

     

    Thursday, July 01, 2010 6:39 PM

Answers

  • Hi dex3703xxx,

    we had this problem too. The reason is that the TextBlock is inside a DataTemplate, and WPF hides these TextBlocks intentionally to UI Automation to improve performance. We solved it by replacing all TextBlocks that are inside DataTemplates with a simple custom TextBlock that overrides the default TextBlockAutomationPeer.

    public class AutomationTextBlock : TextBlock
    {
     protected override AutomationPeer OnCreateAutomationPeer()
     {
      return new CustomTextBlockAutomationPeer(this);
     }
    
     private class CustomTextBlockAutomationPeer : TextBlockAutomationPeer
     {
      public CustomTextBlockAutomationPeer(TextBlock textBlock)
       : base(textBlock)
      {
      }
    	
      protected override bool IsControlElementCore()
      {
       return true;
      }
     }
    }
    

    Hope this helps,

    Michael

    Thursday, July 08, 2010 1:16 PM
  • Thanks. I solved it by making the Textblocks into labels. Your explanation is exactly what we were experiencing. I'll definitely note this for the future.
    Thursday, July 08, 2010 8:34 PM

All replies

  • Hi dex3703xxx,

    Is there an item of this template shown? It is possible that the related item does not exist. There is another tool via which we can get the whole visual tree in runtime: snoop. You can download it from:
    http://snoopwpf.codeplex.com/

    Regards,
    Aland Li


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    Thursday, July 08, 2010 12:14 PM
    Moderator
  • Hi dex3703xxx,

    we had this problem too. The reason is that the TextBlock is inside a DataTemplate, and WPF hides these TextBlocks intentionally to UI Automation to improve performance. We solved it by replacing all TextBlocks that are inside DataTemplates with a simple custom TextBlock that overrides the default TextBlockAutomationPeer.

    public class AutomationTextBlock : TextBlock
    {
     protected override AutomationPeer OnCreateAutomationPeer()
     {
      return new CustomTextBlockAutomationPeer(this);
     }
    
     private class CustomTextBlockAutomationPeer : TextBlockAutomationPeer
     {
      public CustomTextBlockAutomationPeer(TextBlock textBlock)
       : base(textBlock)
      {
      }
    	
      protected override bool IsControlElementCore()
      {
       return true;
      }
     }
    }
    

    Hope this helps,

    Michael

    Thursday, July 08, 2010 1:16 PM
  • Thanks. I solved it by making the Textblocks into labels. Your explanation is exactly what we were experiencing. I'll definitely note this for the future.
    Thursday, July 08, 2010 8:34 PM
  • I can't believe that TextBlock(s) inside of templates aren't available to automation!

    In my opinion, this is huge oversight by Microsoft. In our application, which we are now trying to automate, we heavily favor TextBlock(s) over Label(s) since Label(s) are more heavy weight ... and I speculate that our WPF application is far from the only application to make this decision.

    How does the above ... improve the general app performance? Or, are you suggesting that it improves the automation performance?

    This is quite an annoying issue, and I think it should be changed!

    • Proposed as answer by Cory Plotts Friday, November 18, 2011 8:27 PM
    Friday, November 18, 2011 8:26 PM