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>
Sadegh
  • 865
  • 1
  • 23
  • 47
ChrisMcLellan
  • 613
  • 1
  • 5
  • 13
  • What made you pick Label instead of Button? Any special functionality weights in favour of Label? – Maximus Jul 20 '15 at 09:41
  • @Maximus 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. – ChrisMcLellan Jul 20 '15 at 09:45
  • And Button is not capable of this? – Maximus Jul 20 '15 at 09:56
  • @Maximus We could put a button in however we need the label to show the number of orders completed. – ChrisMcLellan Jul 20 '15 at 09:59
  • 3
    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. – Maximus Jul 20 '15 at 10:01

2 Answers2

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.

Max Young
  • 1,522
  • 1
  • 16
  • 42
Maximus
  • 3,458
  • 3
  • 16
  • 27
  • This is not an answer for running/deployed apps where changing the design is out of scope at the moment. – Veverke Feb 25 '20 at 10:54
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.
Haphil
  • 1,180
  • 1
  • 14
  • 33
  • Option (1) is supposed to work in WPF only ? Can you give or point to an example of option (2) ? – Veverke Feb 25 '20 at 11:01
  • 1
    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.. – Haphil Apr 14 '20 at 06:37