How to expose properties of WPF UI-Elements programatically

Experiences, small talk, and other automation gossip.
Samuel
Posts: 14
Joined: Tue Nov 12, 2013 10:50 am
Location: Germany (BW)

How to expose properties of WPF UI-Elements programatically

Post by Samuel » Thu Dec 05, 2013 9:49 am

Hi all,

I would like to get a better understanding on how difficult it is for a programmer to expose properties of WPF UI-Elements which are not exposed by default. This can happen with custom controls that aren´t just derived from common controls, but have unique properties built into them. I noticed even some common UI-Elements like a Ellypse is also not accessible by default.

So, the question is: What would a programmer have to do so that the application exposes additional properties during runtime like for example if the Fill property of a Ellypse should be accessible for testing?

I asked a programmer at my office to look into this and his idea was to use the Elypse as custom control and to expose necessary properties for this custom control, but we couldn´t figure out exactly how.

The code for the custom control so far looks like:

Code: Select all

public partial class AUIEllipseUserControl : UserControl
    {
        protected override AutomationPeer OnCreateAutomationPeer()
        {
            return new AUIEllipseUserControlAutomationPeer(this);
        }

        public AUIEllipseUserControl()
        {
            InitializeComponent();          
        }

        private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            AUIEllipseUserControl aui = d as AUIEllipseUserControl;
            if (aui != null && (e.OldValue != e.NewValue))
            {
                aui.InnerElipse.Fill = e.NewValue as SolidColorBrush;                
            }
        }
        
        public static readonly DependencyProperty EllipseFillProperty = DependencyProperty.Register("EllipseFill", typeof(SolidColorBrush), typeof(AUIEllipseUserControl), new FrameworkPropertyMetadata(OnPropertyChanged));

        public SolidColorBrush EllipseFill
        {
            get { return this.GetValue(EllipseFillProperty) as SolidColorBrush; }
            set { this.SetValue(EllipseFillProperty, value); }
        }

        private class AUIEllipseUserControlAutomationPeer : FrameworkElementAutomationPeer
        {

            public AUIEllipseUserControlAutomationPeer(FrameworkElement owner)
                : base(owner)
            {
                if (!(owner is AUIEllipseUserControl))
                    throw new ArgumentOutOfRangeException();
            }

            protected override string GetClassNameCore()
            {
                return "AUIEllipseUserControl";
            }

            protected override AutomationControlType GetAutomationControlTypeCore()
            {
                return AutomationControlType.Custom;
            }

            protected override bool IsContentElementCore()
            {
                return true;
            }

            protected override bool IsControlElementCore()
            {
                return false;
            }
        }
    }
If any programers are here maybe you can help us fix the code or show a alternative.
By the way: We´ve also asked at MSDN, but the question seems to be too uncommon for regular programmers who haven´t already worked with UI-Automation.

While researching we found some usefull info sources that might also be of use to other people researching this topic:
Info from MSDN
More Info from MSDN
Example: NumericUpDown Custom Control with Theme and UI Automation Support Sample
Example: Custom Controls and UI Automation

mzperix
Posts: 137
Joined: Fri Apr 06, 2012 12:19 pm

Re: How to expose properties of WPF UI-Elements programatically

Post by mzperix » Thu Mar 13, 2014 11:20 am

Does this post help you?

http://www.ranorex.com/forum/wpf-custom ... t4748.html

It's basically a workaround. You just use the already exposed ItemStatus property to contain the information you need.