MVVM pattern in MS VS 2019 WPF C# ... please be very gently :)

JerryM 1,121 Reputation points
2021-05-08T13:00:04.743+00:00

Hello,

I need several explanations please. :) I have a simple example code in MS VS 2019 C# with WPF - pattern MVVM:

There is the source code:

https://drive.google.com/file/d/1ea-K_BJYvED8S-87YxQZR4aiK0AJw2zP/view?usp=sharing

1/ The code had entry point in file "CommandDemo.xaml.cs".

2/ On row 26 is a command: "this.DataContext = new CommandDemoViewModel();" and this command creates the instance of the class "CommandDemoViewModel".

3/ If I try to debug whole the code step by step from the row 26 there is as the first launched the getter in the property "public ICommand ShowCommand" in the file "CommandDemoViewModel". After that the "RelayCommand" class is created and after the command "return showCommand;" of the getter the debugger jumps to the file "RelayCommand.cs" and the class "RellayCommand" and inside the class is called the "public event EventHandler CanExecuteChanged" and is added an event using command "add".

4/ Can someone very gently explain me WHO is caller of the ""public event EventHandler CanExecuteChanged: add" ?? is it done by an internal processes of the WPF ??? which are hidden from me ?

5/ Who/What is caller of the getter of the property "public ICommand ShowCommand" in class "CommandDemoViewModel" ??

6/ and to where is passed "the control" after the command "return showCommand;" in the "public ICommand ShowCommand" property ???

7/ Is it possible to omit the line "showCommand = new RelayCommand(a);" int the "public ICommand ShowCommand" property and move everything from the file "RelayCommand.cs" to the "public class CommandDemoViewModel : INotifyPropertyChanged" ???

8/ Is there some detailed manual "for dummies" of the internal processes of the WPF ? I would like to understand to it. :)

Jerry

XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
786 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaisyTian-1203 11,621 Reputation points
    2021-05-10T03:25:53.993+00:00

    Q: Can someone very gently explain me WHO is caller of the ""public event EventHandler CanExecuteChanged: add" ?
    A: A C# event has an add and a remove method (similar to a property's get and set methods). The CanExecuteChanged occurs when changes occur that affect whether or not the command should execute, and also notifies any command sources that are bound to that ICommand that the value returned by CanExecute has changed.

    Q: Who/What is caller of the getter of the property "public ICommand ShowCommand" in class "CommandDemoViewModel" ?
    A: You Bind ShowCommand to your Button Command ( <Button x:Name="btnShow" Content="Show" Command="{Binding ShowCommand}" HorizontalAlignment="Left" Margin="20" VerticalAlignment="Top" Width="100"/> ), ButtonBase.Command Property gets or sets the command to invoke when this button is pressed.

    Q: to where is passed "the control" after the command "return showCommand;" in the "public ICommand ShowCommand" property ?
    A: It should invoke the Command Set method for your Button.

    Q: Is it possible to omit the line "showCommand = new RelayCommand(a);" ...
    A: You should not omit showCommand = new RelayCommand(a);, if you omit in your project, the ShowCommand will not invoke CanExecute and Execute.

    Q: Is there some detailed manual "for dummies" of the internal processes of the WPF ? I would like to understand to it. :)
    A: There is a documentation on UWP MVVM that you can use to understand the WPF MVVM schema.

    Do my answer give you help? If they don't, please let me know and give me more detailed description of your questions.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful