A
A
Alex Green2017-02-16 10:59:46
.NET
Alex Green, 2017-02-16 10:59:46

How are events organized in WPF?

Hello!
When we create a new window using XAML markup, a tree of elements is built in the window's constructor inside the InitializeComponent() method;
Elements inside a window can have one event handler added directly in the XAML code.
I work with a ready-made information system that has many windows. Each window has several buttons. Each of the buttons already has an event handler (Click), which is added directly in the XAML code. I have a function that needs to be executed before each button is clicked.
So the question is, how can I add my event handler so that it runs before the one already written in the markup?
Also, it would be interesting to know at what point event handlers are added to element events if they are written in XAML.
I hope for your help..

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Grekhov, 2017-02-23
@deadpoolweid

1 Option " General information about routed events "
2 Option is to make a service with the functionality you need, inject it into each window and add a method call to each handler
3 Option to use code post processing: Fody and for example MethodDecorator or MrAdvice , look at the page Fody repository, there is a large list of handlers.
Which option is better depends on what you're doing.

A
Alex Green, 2017-02-24
@deadpoolweid

<StackPanel Button.Click="SomeButtonOnClick">
    <Button Name="SomeButton1" />
    <Button Name="SomeButton2" />
    <Button Name="SomeButton3" />
...
</StackPanel>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question