Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I register single event listener for all buttons

How can I listen to all button clicks in my WPF app. Maybe include some of the checkboxes or so but ideally I do not want to have extra event handler.

I want to collect initial statistics to know how people use our application. I want to make sure I do not interfere with any other event handling functionality.

In application new windows are being opened and closed with their own buttons so I can not do it statically.

Alternatively is there any common way to collect usage statistics from WPF apps.

like image 270
Marek Avatar asked Jul 18 '13 12:07

Marek


People also ask

How do you add multiple functions to a single event listener?

We can invoke multiple functions on a single event listener without overwriting each other. To do this we simply call the addEventListener() method more than once with a different function. In the example above, we add another event listener for the same event on the same button.

Can you add an event listener to a query selector all?

To add an event listener to the results from the querySelectorAll method: Use the forEach() method to iterate over the collection of elements. Call the addEventListener() method on each element in the collection.


1 Answers

How can I listen to all button clicks in my WPF app?

You can hook events to all objects for a type by using the EventManager:

EventManager.RegisterClassHandler(typeof(Button), Button.ClickEvent, new RoutedEventHandler(Button_Click));

private void Button_Click(object sender, RoutedEventArgs e)
{

}

In application new windows are being opened and closed with their own buttons so I can not do it statically.

If you create these Windows in WPF, you can hook into the Window events such as Closed, Sizechanged and Got/LostFocus(). If these Windows are not WPF/Winform based you can use a ShellHook

like image 61
dsfgsho Avatar answered Nov 12 '22 05:11

dsfgsho