MartinZPetrov
MartinZPetrov

Reputation: 316

How to programmatically click a button in WinRT?

How is this possible for WinRT?

I have read the answer for WPF How to programmatically click a button in WPF?, but does not really help me.

Anyone solved such an issue?

Upvotes: 0

Views: 711

Answers (1)

Strifex
Strifex

Reputation: 862

First, I'd like to say if you want the button to "appear to have been pressed (in terms of animation/highlight effects) this won't help you but otherwise it should.

My Advice to you would be to follow the Model-View-ViewModel (MVVM) design pattern when designing your application if you haven't already. That way instead of calling the "button" click you can simply execute the method in your viewmodel that would normally be bound to that click.

Example:

You create a model class representing data in your database. You create a view (page/window) with buttons and other UI elements on it. You create a ViewModel class that has a series of public methods and collections.

Now in the XAML for the View, you bind the ViewModel as your DataContext and bind the public properties of the ViewModel to your collections (ItemSource for a ListBox being bound to an ObservableCollection is on example). You can create public methods that are "commands" and bind them your buttons so that when the button click event is fired, the command in the view model is executed. Now for all your unit tests and for any other reason you might want to programmatically "click" the button, you can simply call the associated methods in the ViewModel and never worry about what the actual View is doing.

Upvotes: 1

Related Questions