Why is it not recommended to use the event in C # project with WPF and MVVM?

2

I'm very new to WPF I'm doing a project using MVVM and Entity Framework and it was suggested not having any Click tails event for a button in the code behind a window, for what reason? What is the alternative to replace the below event without breaking the suggested rules?

private void Button_Click(object sender, RoutedEventArgs e)
    {
        WindowMenuPrincipal wmp = new WindowMenuPrincipal();
        wmp.ShowDialog();
    }
    
asked by anonymous 04.09.2017 / 23:15

1 answer

5

This has to do with MVVM and not well with WPF. Of course, WPF usually uses MVVM, but nothing prevents it from doing through event handlers and abandoning MVVM. There are people who think it's sacrilege, but the mechanism exists and is valid if you know what you're doing. So understand better how MMVM works (it's a lot more complicated than these links , I recommend buying a good book):

MMVM preaches that the visual presentation should be completely isolated from presentation control, so view is created by a designer or UX professional and the form of the "screen" works is created by a programmer. When you use event handlers in code behind it just blends things. MVVM preaches the use of the Command design pattern to deal with the actions that the screen triggers or needs to update.

About Command:

05.09.2017 / 00:02