Is it possible to Click a label control on WPF/WinForm App using Microsoft Automation UI
Asked Answered
E

2

8

On our application we have a clickable label which will pop up another window. I have been reading through the Microsoft Automation UI documentation, and can't find a way in which I'm able to click a label control.

I know that I can't use the Invoke Pattern as this only deals with Button controls.

Below is the code from the XAML that we have

 <Label Name="LblCompleteOrdersCount" Content="{Binding CompleteOrders, Mode=OneWay}" Margin="434,45,0,0" Height="62" Width="170" Padding="0" HorizontalAlignment="Left" VerticalAlignment="Top" VerticalContentAlignment="Top" FontSize="56" FontWeight="Bold">
    <Label.InputBindings>
        <MouseBinding Command="{Binding Path=CompleteOrdersCommand}" MouseAction="LeftClick" />
    </Label.InputBindings>
 </Label>
Espionage answered 20/7, 2015 at 9:9 Comment(5)
What made you pick Label instead of Button? Any special functionality weights in favour of Label?Melanous
@Melanous The Label is used to show the number of complete orders that we have. Our business needed a way in which they could easily see the number of complete orders (and a summary of each) so they could export these orders to an excel sheet.Espionage
And Button is not capable of this?Melanous
@Melanous We could put a button in however we need the label to show the number of orders completed.Espionage
Use then Button, override default Template and make it look like Label. I see no point in messing around if Label provides nothing more than Button but it lacks from UI Testing support.Melanous
M
6

My hint is to use Button. It derives from ICommandSource interface and therefore you can effortlessly use Command to associate Button with handler. Ask yourself: what functionality does a Label provide that a Button does not? Better look? Override default Button's Template and you will get equivalent look. As long as you do not take advantage of any additional functionality of Label I see no point is messing around with it.

Melanous answered 20/7, 2015 at 10:8 Comment(1)
This is not an answer for running/deployed apps where changing the design is out of scope at the moment.Strangulate
R
5

You got 3 options to solve this problem from my point of view:

  1. UI Automation approach is to override the AutomationPeer of your component and return a ButtonAutomationPeer in your case. The big advantage here is beeing able to model every custom behaviour required. Microsoft's docu for further reading
  2. Use the ClickablePoint of your Label, and combine it with User32.dll (to fire the actual mouseclick on the provided coordinates). - see also microsoft mouse_event function docu for further details - This solution does not require any changes in your app but faces the following drawback: you won't be able to automate several apps simultaneously, if you don't build proper sub-routines on your own and you have to take care to keep the Application in the foreground (you may use the ShowWindow function from User32.dll for that).
  3. As already suggested by Maximus use a Button and make it look like a Label. I agree with him, that it should be a valid workaround in our case.
Rothko answered 23/7, 2015 at 6:50 Comment(2)
Option (1) is supposed to work in WPF only ? Can you give or point to an example of option (2) ?Strangulate
Hi, regarding (1) I only used this for WPF, not sure what to do for winforms -> add new SO question? regarding (2) I am sorry, all those years I referred to the wrong .dll name,edited my post and added a microsoft docu link for you regarding the relevant function..Rothko

© 2022 - 2024 — McMap. All rights reserved.