WPF and MVVM: Advanced Model Treatment

In this course, you learn how to use Model-wrappers in your MVVM-application for several scenarios: to highlight changed fields, to highlight invalid fields, to display validation errors, to enable the save button only if there are changes, and much more.
Course info
Rating
(226)
Level
Intermediate
Updated
Nov 18, 2015
Duration
6h 54m
Table of contents
Introduction
Notifying About Model Changes
Tracking Model Changes
Displaying Model Changes
Validating the Model
Displaying Validation Errors
Generating Model-wrappers with T4
Description
Course info
Rating
(226)
Level
Intermediate
Updated
Nov 18, 2015
Duration
6h 54m
Description

In many modern architectures, your client-application just receives simple Models from the server. These Models often don't have the logic you need in your application. It starts already with the missing information if the Model is changed or not. Without that information, you can't enable or disable the save-button based on the fact if the user has changed the Model or not. This is just one problem that is solved by the powerful Model-wrappers that we create in this course. These Model-wrappers will contain the typical functionality you need in your application: change notification, change tracking, and validation. You learn how to build these Model-wrappers by using Test Driven Development (TDD). And, as this course is about WPF and MVVM, you learn how to use the Model-wrappers in your MVVM-application. You learn how to highlight changed fields with a different background-color, how to display the original value of a changed field in the tooltip, how to highlight invalid fields, how to display validation errors, how to enable the save button only if there are changes, how to check for changes when closing a tab or even the application, how to reset the Model to its original state, and much more. Throughout this course, you learn how to extend a typical MVVM-based WPF-application called FriendStorage with all this functionality.

About the author
About the author

Thomas is a well-known author, software developer, and speaker from Germany. He specializes in .NET, C#, TypeScript, XAML, and Azure.

More from the author
.NET Standard: Getting Started
Intermediate
2h 44m
May 14, 2018
More courses by Thomas Claudius Huber
Section Introduction Transcripts
Section Introduction Transcripts

Introduction
Welcome to Pluralsight. My name is Thomas Claudius Huber, and you are watching the first module of the course, WPF and MVVM: Advanced Model Treatment. In a WPF client you often receive plain-old CLR objects as models. Those models usually don't have the functionality you need in your user interface. This course is about creating model wrappers for those plain-old CLR objects that have all the functionality you need. This includes change notification, change tracking, and validation. So, for example, for the change tracking, you'll learn how to track the changes made to each single field of the model, and when we track those changes we also store the original value in the model-wrapper, and as this course is about WPF and MVVM, you will learn how to use this model-wrapper in your ViewModel and how to create the styles to display changed fields with a different background color and how to create the data bindings to display the original value of a changed field in the tooltip. Alright, so let's look now at the course outline and I'll tell you a bit more about the model-wrappers and the UI parts you'll learn in this course.

Validating the Model
Welcome to the model, Validating the Model. My name is Thomas Claudius Huber, and in this module I will show you how you can add validation logic to our model wrappers. So, for example, the user should not be able to save a Friend without Firstname. As you might remember from the beginning of this course, the model-wrapper functionality is split up into three parts. We have change notification, change tracking, and validation. As you know, we are done with the change notification and with the change tracking. Now let's implement the validation part. So let's have a look at the module outline. In this module, we extend the FriendWrapper with validation logic. As usual, we try to put as much logic as we can into the ModelWrapper base class, and we will also ensure the functionality with unit tests. The validation that we build in this module is based on the INotifyDataErrorInfo interface. To specify the rules for the validation, we will use data Annotations. Okay, so far to the module outline. Now let's start with the interface INotifyDataErrorInfo.

Displaying Validation Errors
Welcome to the module, Displaying Validation Errors. My name is Thomas Claudius Huber. In the previous module, we added the validation logic to our model-wrappers. Now in this module we want to display the validation errors to the user so that he knows what he actually needs to do. Okay, so let's have a quick look at the module outline. In this module you will learn how to highlight invalid fields with a different background color. You will also learn how to display the error message in the tooltip, so the user should be able to find out what's wrong with such an invalid field. Beside those two tasks, we will also create custom error templates. An error template allows us to display additional UI elements if a control is invalid. Okay, before we start with those tasks, let me give you an overview of validation errors in the user interface.

Generating Model-wrappers with T4
Welcome to the module, Generating Model-wrappers with T4. My name is Thomas Claudius Huber, and in this module I will show you how you can generate the boilerplate code to wrap our models. In the FriendStorage application, we have, for example, the Friend model. The Friend model has several properties. One of these properties is the FirstName property. For that Friend model, we have created a FriendWrapper that has also a FirstName property. In addition, it has the properties FirstNameOriginalValue and FirstNameIsChanged. So when you look at these properties of the FriendWrapper, it's clear that you can generate all these properties when you know the Friend model. So what we will do in this module, we will build a Generator with T4 that reads the Friend model and then generates our wrapper. So let's have a quick look at the module outline. We start his module with the T4 basics. Then we will create a T4 template to generate the Model-wrappers of the FriendStorage application. After we have generated the Model-wrappers, including simple properties, complex properties, and collection properties, we will add support for partial classes that will contain the validation logic. Okay, so let's start with the T4 basics.