Intro to Model-View-ViewModel (MVVM) Pattern for WPF in C#
Why Stack Overflow recommends it
Stack Overflow users link it as a beginner-friendly, plainly spoken introduction to MVVM whenever someone asks for a tutorial or runs into basic WPF binding and viewmodel problems.
Recommended by 5 people across 5 different months, 2013 to 2017.
What it is
A 23-minute introductory tutorial by David Anderson (DCOM Engineering) on the Model-View-ViewModel pattern for WPF in C#. It covers the basic fundamentals of the pattern, its purpose, and how to apply it, with downloadable demo source code.
What people said
Strong rec B.K. · 2015-03 · Stack Overflow · on “Beginner tutorial on MVVM?”
I usually would say that this is off-topic, but this question is being asked so often, that it probably deserves an answer. I swear by this gentleman and everyone I've recommended him to has been satisfied: DCOM Engineering, LCC (YouTube) : [LINK] You will find two MVVM series. The first is the Introduction to Model-View-ViewModel Pattern [this video] series and the other is the Enterprise MVVM [LINK] series. he posted the second one very recently. I would start with the first one to get a very clear understanding and move to the second one to solidify what you've learned. He speaks clearly, in "plain-enough" language and does a very good job of covering topics without convolution involving finer
Recommends tsragravorogh · 2013-04 · Stack Overflow · on “Handle WPF button click and enable disable”
Command will be executed only when the button will be clicked on. If you need to do something to the button then you should do it to the Window that contains the button (assuming your button is in a Window). Now if you want to stick with the MVVM pattern then you should not use the Window.OnLoaded, because that would put code in your code behind. One option is to use System.Windows.Interactivity, which you have download seperately. Here is what it will look like: <Window x:Class="..." xmlns=" [LINK] " xmlns:x=" [LINK] " xmlns:i="clr -namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"> <i:Interaction.Triggers> <i:EventTrigger EventName="Loaded"> <i:InvokeCommandActi
Recommends Grant Winney · 2014-02 · Stack Overflow · on “add color to content c#”
Use the Foreground [LINK] property: if (ca >= ps) { label_result.Content = "Passed!!"; label1.Foreground = Brushes.Blue; } else { label_result.Content = "Failed!!"; label1.Foreground = Brushes.Red; } When you're just starting out, it's easier to use code-behind. But you'll definitely want to learn about data binding and the MVVM pattern sooner rather than later. You can find a lot of excellent resources online, such as this one [this video] and this one [LINK] . (randomly picked from top google results, but both rated well)
Recommends FearlessCoward · 2014-05 · Stack Overflow · on “Lack of knowledge to initializing viewmodel”
If you or anybody else, who's new to the MVVM, gets stuck here, for example at the " INotifyPropertyChanged could not be found ". I recommend trying some example-MVVM's or tutorials. Some I found useful: [LINK] [this video]
Recommends user2153378 · 2014-09 · Stack Overflow · on “MVVM binding is not updating UI element”
You might want to take a look at the Binding.Elementname [LINK] . Maybe you need to watch this video [this video] (one of 3), it helped me out allot. Here is also a really great (and entertaining) video [LINK] , it takes a different approach.
Cites LamaCoder · 2014-05 · Stack Overflow · on “Trying to: Avoid static instance as datacontext”
OK, your mentioning "new windows", however Im trying to create pages, that should be displayed inside a frame. And I have problems getting how to set that up properly, since the frame is not the same as a Window if I got it right. And also a question, when you say view your talking about the code behind of the xaml? If so, I have heard that it should only contain the datacontext, and it´s up to the viewmodel, to actually create the logic. Source: [this video]
Cites Robert J. · 2014-06 · Stack Overflow · on “Adding a textbox on button click in c# wpf”
Here is what I do when writing MVVM application. Create class that will handle your commands (this is just a quick sample): class CommandHandler:ICommand { private Action action; private Func<bool> isEnabled; public CommandHandler(Action _action, Func<bool> _isEnabled) { action = _action; isEnabled = _isEnabled; } public bool CanExecute(object parameter) { return isEnabled(); } public event EventHandler CanExecuteChanged; public void Execute(object parameter) { action(); } } Now under you ViewModel, you have to create tow things: 1) Create method that you will call with your button click like the following Public void ButtonWasClicked() { //do something here } 2) Create actual command privat
Cites smehrlapf · 2017-11 · Stack Overflow · on “MVVM Model class with Thread updating properties”
I just started with the MVVM design pattern and built my own small project based on the example shown in this youtube video [this video] , source is found here [LINK] . In addition to the example I created a new thread in the Model which updates some of the Model properties every second. It worked well; the text boxes in my View were updated every second like expected. However, the example implemented INotifyPropertyChanged on the Model and bindings were made from the View to the properties of the Model. If I understood the MVVM pattern corretly, the latter described approach is against the MVVM pattern, the INotifyPropertyChanged should be implemented on the ViewModel instead and the View should