Microsoft Dot Net Master

Microsoft Dot Net Master
Microsoft Dot Net Master

Tuesday, July 31, 2012

UI Automation with WPF

How to make TextBlock within a DataTemplate visible to UI Automation

WPF intentionally hides TextBlocks that are inside a DataTemplate to improve performance. To make them visible, you have to replace them by a Label which can be a performance hit, or you make a special UiAutomationTextBlock that overrides this behavior:
 
public class UiAutomationTextBlock : TextBlock
{
    protected override AutomationPeer OnCreateAutomationPeer()
    {
        return new ModifiedTextBlockAutomationPeer(this);
    }
 
    private class ModifiedTextBlockAutomationPeer : TextBlockAutomationPeer
    {
        public ModifiedTextBlockAutomationPeer(TextBlock textBlock)
            : base(textBlock)
        { }
 
        protected override bool IsControlElementCore()
        {
            return true;
        }
    }
}
 
 
The Idea for that solution was found here

No comments:

Post a Comment