Catel 3.9 Documentation

download Catel 3.9 Documentation

of 320

description

Catel 3.9 Documentation

Transcript of Catel 3.9 Documentation

  • 1. Catel documentation Home . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

    1.1.1 Why Catel? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71.1.2 Platform support explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81.1.3 Introduction to data objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81.1.4 Introduction to MVVM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

    1.1.4.1 MVVM framework comparison sheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161.1.4.2 Different interpretations of MVVM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191.1.4.3 Validation in model or view model? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201.1.4.4 Introduction to MVVM and models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211.1.4.5 Creating view models with Catel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211.1.4.6 Introduction to services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281.1.4.7 Introduction to the nested user controls problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301.1.4.8 Introduction to unit testing in MVVM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

    1.1.5 Introduction to MVC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391.2 FAQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

    1.2.1 General . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391.2.2 MVVM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

    1.3 Setup, deployment and projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441.3.1 Getting prerelease (beta) versions via NuGet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441.3.2 Stepping through the code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461.3.3 Compiling from source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461.3.4 Code snippets & templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

    1.3.4.1 Using the code snippets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461.3.4.2 Using the item templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471.3.4.3 Using the project templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

    1.3.5 Updating to a new version via NuGet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501.4 Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

    1.4.1 Quick introduction for developers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511.4.2 Getting started with WPF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

    1.4.2.1 Creating the project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541.4.2.2 Creating the models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571.4.2.3 Serializing data from/to disk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611.4.2.4 Creating the view models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651.4.2.5 Creating the views (user controls) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691.4.2.6 Creating the views (windows) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711.4.2.7 Hooking up everything together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731.4.2.8 Finalizing the application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80

    1.5 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 861.5.1 WPF Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87

    1.5.1.1 Advanced WPF example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 871.5.1.2 Authentication example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 891.5.1.3 Browser application example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 901.5.1.4 Master/detail example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 911.5.1.5 Multilingual example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 911.5.1.6 MVVM communication styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 921.5.1.7 Person application WPF example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 931.5.1.8 Validation example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 941.5.1.9 Viewmodel lifetime example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95

    1.5.2 Silverlight examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 961.5.2.1 Advanced Silverlight example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 961.5.2.2 Navigation example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 991.5.2.3 Nested user controls example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1001.5.2.4 Person application Silverlight example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101

    1.5.3 Windows Phone examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1021.5.3.1 Bing maps example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1021.5.3.2 Sensors example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1041.5.3.3 Shopping list example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106

    1.6 Performance considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1071.7 Catel.Core . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110

    1.7.1 Argument checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1101.7.2 Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1111.7.3 Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1141.7.4 Data handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116

    1.7.4.1 ObservableObject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1161.7.4.2 DispatcherObservableObject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1171.7.4.3 ModelBase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1181.7.4.4 Using ModelBase as base for entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1211.7.4.5 Advanced property change notifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1221.7.4.6 WCF services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123

    1.7.5 Exception handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1241.7.6 IoC (ServiceLocator and TypeFactory) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128

  • 1.7.6.1 Introductions to IoC components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1291.7.6.1.1 Introduction to the ServiceLocator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1291.7.6.1.2 Introduction to the TypeFactory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1301.7.6.1.3 Introduction to DependencyResolver and DependencyResolverManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1331.7.6.1.4 Dependency injection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1361.7.6.1.5 Ensuring integrity of the ServiceLocator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141

    1.7.6.2 Automatic type registration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1431.7.6.2.1 Automatically registering types using attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1431.7.6.2.2 Automatically registering types using conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144

    1.7.6.3 Setting up the ServiceLocator using configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1451.7.6.4 Replacing the default components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147

    1.7.7 Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1481.7.7.1 Customizing listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1491.7.7.2 Batch log listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1501.7.7.3 Integration with external loggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

    1.7.7.3.1 Log4net . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1521.7.7.3.2 NLog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152

    1.7.7.4 Writing logs to disk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1531.7.7.5 Creating log listeners via configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1541.7.7.6 Anotar.Catel.Fody . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155

    1.7.8 Messaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1561.7.8.1 MessageBase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1561.7.8.2 Message mediator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1571.7.8.3 Messaging via attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158

    1.7.9 Multilingual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1591.7.10 Parallel invocation and tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1621.7.11 Preventing memory leaks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163

    1.7.11.1 Change notification wrapper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1631.7.11.2 Weak events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164

    1.7.12 Reflection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1661.7.13 Scoping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1671.7.14 Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167

    1.7.14.1 Introduction to serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1671.7.14.2 Specifying what gets serialized . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1691.7.14.3 Customizing serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172

    1.7.15 Thread safe code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1751.7.16 Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178

    1.7.16.1 Validation via validate methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1791.7.16.2 Validation via data annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1791.7.16.3 Validation via special model validators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1801.7.16.4 Validation via IValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1811.7.16.5 Using the validation context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1841.7.16.6 Getting a summary of validation results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1851.7.16.7 Deferring validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185

    1.8 Catel.MVVM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1861.8.1 Auditing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1861.8.2 Behaviors & triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188

    1.8.2.1 Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1881.8.2.2 DelayBindingUpdate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1901.8.2.3 DoubleClickToCommand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1911.8.2.4 EventToCommand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1911.8.2.5 Focus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1931.8.2.6 FocusFirstControl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1941.8.2.7 KeyPressToCommand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1941.8.2.8 MouseInfo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1951.8.2.9 Navigate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1951.8.2.10 NumericTextBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1961.8.2.11 SelectTextOnFocus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1961.8.2.12 UpdateBindingOnPasswordChanged . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1971.8.2.13 UpdateBindingOnTextChanged . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197

    1.8.3 Commands & events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1981.8.3.1 Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1991.8.3.2 Application-wide commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2001.8.3.3 Asynchronous commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2011.8.3.4 Commands authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2031.8.3.5 Hooking a command to validation automatically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204

    1.8.4 Converters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2051.8.5 Designers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2071.8.6 Handling application initialization parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2101.8.7 Locators and naming conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211

    1.8.7.1 ViewModelLocator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2121.8.7.2 ViewLocator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213

  • 1.8.7.3 UrlLocator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2151.8.7.4 Naming conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216

    1.8.8 Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2171.8.8.1 AccelerometerService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2181.8.8.2 CameraService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2191.8.8.3 CompassService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2211.8.8.4 GyroscopeService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2221.8.8.5 LocationService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2241.8.8.6 MessageService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2251.8.8.7 NavigationService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2261.8.8.8 OpenFileService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2291.8.8.9 PleaseWaitService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2291.8.8.10 ProcessService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2311.8.8.11 SaveFileService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2321.8.8.12 SchedulerService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2321.8.8.13 SelectDirectoryService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2331.8.8.14 SplashScreenService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2341.8.8.15 UIVisualizerService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2361.8.8.16 VibrateService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2381.8.8.17 ViewExportService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239

    1.8.9 View models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2391.8.9.1 Creating a basic view model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2401.8.9.2 Creating a view model that watches over other view models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2411.8.9.3 Creating a view model with a model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2441.8.9.4 Creating a view model with a model and mappings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2481.8.9.5 Mapping properties from view to view model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2491.8.9.6 Nested view models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2511.8.9.7 Validation in view models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2511.8.9.8 Advanced view models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255

    1.8.9.8.1 Keeping view models alive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2561.8.9.8.2 Exposing properties of a model automatically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2561.8.9.8.3 Determine the view model type dynamically at runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2581.8.9.8.4 Controlling the instantiation of view models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258

    1.8.10 Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2601.8.10.1 DataWindow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2601.8.10.2 UserControl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2631.8.10.3 MVVM behaviors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2681.8.10.4 Validation controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2691.8.10.5 Finding the view of a view model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2701.8.10.6 Using external controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270

    1.8.10.6.1 Using a custom control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2701.8.10.6.2 Using a custom window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274

    1.8.10.7 Advanced information about views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2771.8.10.7.1 DataWindow - under the hood . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2771.8.10.7.2 UserControl - under the hood . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277

    1.9 Catel.Mvc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2831.9.1 Configuring dependency injection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283

    1.10 Catel.Extensions.Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2841.10.1 Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285

    1.10.1.1 StackGrid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2851.10.1.2 TabControl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2861.10.1.3 TraceOutputControl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2861.10.1.4 WatermarkTextBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286

    1.10.2 Pixel shaders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2871.10.3 StyleHelper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2881.10.4 Themes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289

    1.11 Catel.Extensions.CSLA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2901.12 Catel.Extensions.Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290

    1.12.1 Specifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2901.13 Catel.Extensions.DynamicObjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2911.14 Catel.Extensions.EntityFramework5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292

    1.14.1 Using the DbContextManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2921.14.2 Using the repositories and unit of work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293

    1.15 Catel.Extensions.FluentValidation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2951.16 Catel.Extensions.Interception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296

    1.16.1 Method interception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2971.16.2 Property interception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302

    1.17 Catel.Extensions.Memento . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3031.17.1 Memento and collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3041.17.2 Memento and methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3051.17.3 Memento and properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306

    1.18 Catel.Extensions.Prism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307

  • 1.18.1 Declaring modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3101.18.2 Translating or customizing the initialization task messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3101.18.3 Using the bootstrapper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311

    1.19 Catel.Extensions.Wcf.Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3121.20 Catel.Fody . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312

    1.20.1 Weaving properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3131.20.2 Weaving argument checks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3141.20.3 Exposing properties on view models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3151.20.4 XmlSchema generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317

    1.21 Catel.ReSharper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3171.21.1 Checking arguments of a method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3171.21.2 Converting regular properties into Catel properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319

    1.22 Reference documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320

  • Catel documentation HomeWelcome to the documentation of Catel. The framework consists of several projects. Each project has it's reference documentation. The actualtechnical documentation is available in the downloads sections. However, the wiki contains some real-life examples and additional explanation ofsome of the features of Catel.

    Supported platformsLooking for documentation of other versions?Articles or documentation?Explanation about the documentation

    Catel is an application toolkit with the focus on MVVM, and consists of two basic pillars and extensions.

    contains an IoC container, data objects, validation, message mediator, argument checking, etc. This can be used in any applicationCatel.Coreand is not specific for Windows applications.

    Catel.MVVM contains all the MVVM classes such as ViewModelBase, Command, services, etc.

    Catel also provides the following extensions:

    Catel.Extensions.Controls Catel.Extensions.CSLACatel.Extensions.DataCatel.Extensions.EntityFramework5Catel.Extensions.EntityFramework6Catel.Extensions.FluentValidationCatel.Extensions.InterceptionCatel.Extensions.MementoCatel.Extensions.Prism

    Below is a visual representation of the available blocks in Catel:

    Supported platformsThe following platforms are supported by Catel:

    Don't want to read much? Make sure that you at least read the with the absolute basicsquick introduction

  • Looking for documentation of other versions?Catel LATESTCatel 3.8Catel 3.7Catel 3.6Catel 3.5

    Articles or documentation?You have probably also found the articles on The Code Project. These articles are meant to be an introduction to a specific field of techniques thatcan be found inside Catel. If you are looking for documentation, this is the place to be.

    Are you missing documentation?

    If you are missing documentation or feel that documentation is out of date, please let us know and we will will fix it!

    Explanation about the documentationYou can either navigate through the left bar or via the search at the right top. Below are the top parts of the documentation:

    IntroductionFAQSetup, deployment and projectsGetting startedExamplesPerformance considerationsCatel.CoreCatel.MVVMCatel.MvcCatel.Extensions.ControlsCatel.Extensions.CSLACatel.Extensions.DataCatel.Extensions.DynamicObjectsCatel.Extensions.EntityFramework5Catel.Extensions.FluentValidationCatel.Extensions.InterceptionCatel.Extensions.MementoCatel.Extensions.PrismCatel.Extensions.Wcf.ServerCatel.FodyCatel.ReSharperReference documentation

    IntroductionWelcome to the introduction of Catel. Catel is a framework (or enterprise library, just use whatever you like) with data handling, diagnostics,logging, WPF controls, and an MVVM-framework. So Catel is more than "just" another MVVM-framework or some nice Extension Methods thatcan be used. It's more like a library that you want to include in all the XAML applications you are going to develop in the near future.

    For detailed information about platform support, see Platform support explanation

    Don't forget to take a look at the code snippets and project templates, they will make your life much easier

    Note that Catel is primarily meant for Line of Business (LoB) applications

  • It's important to realize that Catel is not just another Extension Methods library, nor only an MVVM-framework, but it is a combination of basic datahandling, useful controls, and an MVVM-framework.

    Why another framework?You might be thinking: why another framework, there are literally thousands of them out there. Well, first of all, thousands of them is quite a lot,let's just say there are hundreds of them. A few years ago, the lead developer of Catel was using serialization to serialize data from/to disk. But,as he noticed, he had to take care of different versions after every release. After every release, he had to take care of the serialization andbackwards compatibility. Also, he had to implement some very basic interfaces (such as INotifyPropertyChanged) for every data object. Then, hedecided to write a base class for data handling which can take care of different versions and serialization by itself, and implements the most basicinterfaces of the .NET Framework out of the box. The article was published on CodeProject as DataObjectBase.Then, he was working on a WPF project with five other developers and needed an MVVM-framework since the application was not using MVVMat the moment. Writing an MVVM-framework was no option because there were so many other frameworks out there. But, after looking at someOpen-Source MVVM-frameworks (such as the excellent Cinch framework, which was the best one we could find), none of them seemed to be areal option. Creating the View Models was too much work, and the View Models still contained lots of repetitive code in, for example, the propertydefinitions. After taking a closer look at the source code of Cinch and other frameworks, the lead developer thought: if we use the DataObjectBasepublished before as the base for a View Model class, it should be possible to create a framework in a very short amount of time.

    Then, all other developers of the team he was working on the project got enthusiastic, and then the whole team decided to merge their personallibraries into one big enterprise library, and Catel was born.

    Why use this framework?Before reading any further, it's important to know why you should use the framework. Below are a few reasons why Catel might be interesting foryou:

    Catel is Open-Source. This way, you can customize it any way you want. If you want a new feature request, and the team does notrespond fast enough, you can simply implement it yourself.The codebase for Catel is available on GitHub. This way, you have the option to either download the latest stable release, or live on theedge by downloading the latest source code.Catel uses unit tests to make sure that new updates do not break existing functionality.Catel is very well documented. Every method and property has comments, and the comments are available in a separate reference helpfile. There is also a lot of documentation available, and in the future, in-depth articles will be written.Catel is developed by a group of talented software developers, and is heavily under development. This is a great advantage because theknowledge is not at just one person, but at a whole group. The developers of Catel all have more than three years of developmentexperience with WPF, and are using Catel in real life applications for at least 2 years.

    Continue readingWhy Catel?Platform support explanationIntroduction to data objectsIntroduction to MVVMIntroduction to MVC

    Why Catel?We care a lot about the freedom you need as a software developer. Most frameworks require a developer to learn its conventions, or use thewhole framework or nothing at all. When we, the developers of Catel, use an external framework, we choose that framework for a specific reason,and dont want to be bothered by all the other superb stuff it has to offer (maybe later, but not now).During the development of Catel, we tried to maintain this freedom aspect which is very important to us. Therefore, all functionality is looselycoupled. Sounds great, but everything is called loosely coupled nowadays. Catel contains a lot of different aspects, such as logging, diagnostics,Reflection, MVVM, user controls, windows, etc. All these aspects are complementary to each other, but the great thing about Catel is that youdecide whether to use just one, some, or maybe all aspects.

    As an example: you have a legacy application and want to use the DataWindow to write simple entry windows, but you are not ready for MVVMyet. No problem, the DataWindow is complementary to MVVM, but does not require it. Therefore, you have all the freedom to use just what youneed, whenever you need it.

    Most frameworks require a bootstrapper that completely decides how your application structure should look like. For example, your Views must

  • have this name, your controls must have that name. Again, in Catel, we wanted to give you the freedom you would expect from a framework.

    The great thing about this freedom is that the different aspects of Catel can be used side-by-side with other frameworks, so you as a developercan use the best framework for every aspect in your application.

    Another nice thing is that Catel is WPF-based. Now you might be thinking: but hey, I use Silverlight! However, the upside of this approach is thatall goodies that are well known in WPF, but not in Silverlight are also brought to Silverlight. For example, in Silverlight there is no automaticcommand re-evaluation. Catel does this out of the box, even in Silverlight.

    Catel offers a solution in the following fields:

    Data handlingMVVMAnd much more which you will find out during the use of Catel!

    Platform support explanationThis page explains in detail what parts of Catel are available on specific platforms.

    NET40 NET45 SL4 SL5 WP7.8 WP8 WIN80 WIN81 PCL

    Catel.Core

    Catel.MVVM

    Catel.Mvc

    Catel.Extensions.Controls

    Catel.Extensions.CSLA

    Catel.Extensions.Data

    Catel.Extensions.EntityFramework5

    Catel.Extensions.FluentValidation

    Catel.Extensions.Interception

    Catel.Extensions.Memento

    Catel.Extensions.Prism

    Introduction to data objectsIt is very important to understand the data objects in Catel because they form the base pillar of all components used by the MVVM framework.

    The ObservableObject classThe ModelBase class

  • 1. 2.

    Creating your first data objectDeclaring properties

    Simple propertiesProperties with property change callback

    Adding validationSaving objects

    Loading an objectSaving an object

    The ObservableObject class

    The ModelBase class

    Creating your first data objectFirst of all, it is very important to realize that you shouldn't bore yourself with writing all the code below yourself. Catel contains lots of codesnippets that allow you to create data objects very easily in a short amount of time.This example shows the simplest way to declare a data object using the class. By using a code snippet, the class is created in just 10ModelBaseseconds.

    Code snippets

    model - Declares a data object based on the classModelBaseSteps

    Create a new class file called .FirstModel.csInside the namespace, use the codesnippet and fill in the name of the class, in this case .model FirstModel

    Code

    This documentation is to be written in the future

    This documentation is to be written in the future

  • /// /// FirstModel class which fully supports serialization, property changednotifications,/// backwards compatibility and error checking./// [Serializable]public class FirstModel : ModelBase{ #region Fields #endregion

    #region Constructors /// /// Initializes a new object from scratch. /// public FirstModel() { }

    /// /// Initializes a new object based on . /// /// that contains theinformation. /// /// . protected FirstModel(SerializationInfo info, StreamingContext context) : base(info, context) { } #endregion

    #region Properties // TODO: Define your custom properties here using the propdata code snippet #endregion

    #region Methods /// /// Validates the field values of this object. Override this method to enable /// validation of field values. /// /// The validation results, add additional resultsto this list. protected override void ValidateFields(ListvalidationResults) { }

    /// /// Validates the field values of this object. Override this method to enable /// validation of field values. /// /// The validation results, add additional resultsto this list. protected override void ValidateBusinessRules(ListvalidationResults) { } #endregion }

  • 1. 2.

    1. 2.

    Declaring propertiesThe next step to learn on the class is how to declare properties. There are several types of properties, and they will all be handled inModelBasethis part of the documentation.

    The class uses a dependency property a-like notation of properties.ModelBase

    Simple propertiesThis example shows how to declare the simplest property. In this example, a string property with a default value will be declared with the use of acode snippet.

    Code snippets

    modelprop - Declares a simple property on a model

    Steps

    Open created in the previous step.FirstModel.csIn the Properties region, use the code snippet , and use the following values:modelprop

    Code snippet item Value

    description Gets or sets the simple property

    type string

    name SimpleProperty

    defaultvalue "Simple property"

    Code

    /// /// Gets or sets the simple property./// public string SimpleProperty{ get { return GetValue(SimplePropertyProperty); } set { SetValue(SimplePropertyProperty, value); }}

    /// /// Register the SimpleProperty property so it is known in the class./// public static readonly PropertyDataSimplePropertyProperty =RegisterProperty("SimpleProperty", typeof(string), "Simple property");

    Properties with property change callbackCode snippets

    modelpropchanged - Declares a simple property on a model with a property changed callback

    Steps

    Open created in the previous step.FirstModel.csIn the Properties region, use the code snippet , and use the following values:modelpropchanged

    Code snippet item Value

    description Gets or sets the callback property

    type string

    name CallbackProperty

  • defaultvalue "Callback property"

    Code

    /// /// Gets or sets the callback property./// public string CallbackProperty{ get { return GetValue(CallbackPropertyProperty); } set { SetValue(CallbackPropertyProperty, value); }}

    /// /// Register the CallbackProperty property so it is known in the class./// public static readonly PropertyDataCallbackPropertyProperty =RegisterProperty("CallbackProperty", typeof(string), "Callback property", (sender, e) => ((FirstDataObject)sender).OnCallbackPropertyChanged());

    /// /// Called when the CallbackProperty property has changed./// private void OnCallbackPropertyChanged(){ // TODO: Implement logic}

    Adding validationIt is very easy to add validation to a class (both the and ). There are several ways, but this getting started guide willModelBase ViewModelBasehandle only the most simple one.

    To enable validation, you must override at least one of the following methods:

  • 1. 2. 3. 4.

    /// /// Validates the field values of this object. Override this method to enable/// validation of field values./// /// The validation results, add additional results tothis list.protected override void ValidateFields(List validationResults){ if (string.IsNullOrEmpty(FirstName)) { validationResults.Add(FieldValidationResult.CreateError(FirstNameProperty,"First name cannot be empty")); }}

    /// /// Validates the field values of this object. Override this method to enable/// validation of field values./// /// The validation results, add additional results tothis list.protected override void ValidateBusinessRules(ListvalidationResults){ if (SomeBusinessErrorOccurs) { validationResults.Add(BusinessRuleValidationResult.CreateError("A businesserror occurred")); }}

    After the validation is implemented into the object, the validation will occur every time a property on the object changes. It is also possible tomanually validate by calling the Validate method.

    There are also other ways to add validation to a data object:

    Validation via data annotations - attributes such as the RequiredAttributeValidation via - custom validation such as IValidator FluentValidation

    The great thing is that Catel will gather all validation results from all different mappings and combine these into the . This contextValidationContextcan be used to query all sorts of validation info about an object.

    Saving objectsSaving and loading objects out of the box has never been so easy. can automatically save/load objects in several ways, suchSavableModelBaseas memory, file in different modes (binary and XML). This example shows that making your objects savable is very easy and does not take anytime!

    Code snippets

    model - Declare a model based on the classModelBasemodelprop - Declare a simple property on a model

    Steps

    Create a new class file called .Person.csInside the namespace, use the codesnippet and fill in the name of the class, in this case .model PersonChange the base class from to .ModelBase SavableModelBaseIn the Properties region, use the code snippet , and use the following values:modelprop

    Note that this is just an introduction, more information about validation can be found in other parts of the documentation

  • Code snippet item Value

    description Gets or sets the name

    type string

    name Name

    defaultvalue "MyName"

    Code

  • /// /// Person class which fully supports serialization, property changed notifications,/// backwards compatibility and error checking./// [Serializable]public class Person : SavableModelBase{ #region Fields #endregion

    #region Constructors /// /// Initializes a new object from scratch. /// public Person() { }

    /// /// Initializes a new object based on . /// /// that contains the information. /// /// . protected Person(SerializationInfo info, StreamingContext context) : base(info, context) { } #endregion

    #region Properties /// /// Gets or sets the name. /// public string Name { get { return GetValue(NameProperty); } set { SetValue(NameProperty, value); } } /// /// Register the Name property so it is known in the class. /// public static readonly PropertyData NameProperty = RegisterProperty("Name",typeof(string), "MyName"); #endregion

    #region Methods #endregion }

    Loading an objectLoading an object is really simple once the class has been created. It is important to use the static method on the class:

    var person = Person.Load(@"c:\person.dob");

  • Saving an objectTo save an object, an instance is required. Then simply call the method.Save

    var person = new Person();person.Name = "John Doe";person.Save(@"c:\person.dob");

    Introduction to MVVMThis part gives an introduction to MVVM in Catel.

    MVVM framework comparison sheetDifferent interpretations of MVVMValidation in model or view model?Introduction to MVVM and modelsCreating view models with CatelIntroduction to servicesIntroduction to the nested user controls problemIntroduction to unit testing in MVVM

    MVVM framework comparison sheetIn this MVVM framework comparison sheet includes as many aspects as possible. However, it might be possible that some were missed. If youhave features that are not included in the comparison sheet, or you find that information is incorrect, please let us know!

    Below is short list of definitions:

    Definition Explanation

    VM View-Model

    Frameworks reviewed

    Framework Description Website

    Catel Catel is more than just an MVVM toolkit. Italso includes user controls and lots ofenterprise library classes. The MVVM toolkitdiffers itself from other frameworks of thesimplicity and solves the "nested usercontrol" problem with dynamic viewmodelsfor user controls

    http://catel.codeplex.com

    Caliburn.Micro A small, yet powerful implementation ofCaliburn designed for WPF, Silverlight andWP7. The framework implements a variety ofUI patterns for solving real-world problems.Patterns that are enabled include MVC,MVP, Presentation Model (MVVM), andApplication Controller.

    http://caliburnmicro.codeplex.com

    Cinch (V2) Cinch is a MVVM framework that exposes anumber of helper classes to allow thedeveloper to quickly get to grips with creatingscalable testable MVVM frameworks asquickly as possible.

    http://cinch.codeplex.com

    MVVM light The MVVM Light Toolkit is a set ofcomponents helping people to get started inthe Model - View - ViewModel pattern inSilverlight and WPF. It is a light andpragmatic framework that contains only theessential components needed.

    http://mvvmlight.codeplex.com

    This comparison sheet is last updated on September 4th, 2012

  • Simple MVVM toolkit Simple MVVM Toolkit makes it easier todevelop Silverlight, WPF and WP7applications using theModel-View-ViewModel design pattern. Thepurpose of the toolkit is to provide a simpleframework and set of tools for getting up tospeed quickly with applications based on theMVVM design pattern. The emphasis is onsimplicity, but it contains everything you needto implement MVVM for real-world line ofbusiness applications.

    http://simplemvvmtoolkit.codeplex.com/

    WAF The WPF Application Framework (WAF) is alightweight Framework that helps you tocreate well structured WPF Applications. Itsupports you in applying a LayeredArchitecture and the Model-View-ViewModel(aka MVVM, M-V-VM, PresentationModel)pattern.

    http://waf.codeplex.com/

    Platform support Catel Caliburn.Micro Cinch (V2) MVVM light Simple MVVM WAFWPF

    Silverlight 4

    Silverlight 5

    Windows Phone7

    Windows Phone8

    WinRT

    Available viaNuGet

    UI features

    Catel Caliburn.Micro Cinch (V2) MVVM light Simple MVVM WAFMVVM specificwindow

    MVVM specificuser control

    Dynamic viewmodel injectionLazy loading ofview models

    View to VMcommunicationvia attributes

    Events whenviews areactually loaded

  • ViewModelBase functionality

    Catel Caliburn.Micro Cinch (V2) MVVM light Simple MVVM WAFINotifyPropertyChanged

    IDataErrorInfo

    IDataWarningInfo

    Support forcanceling data

    Support forconfirming data

    Automaticpropertydefinitions

    Automaticmodelcommunications (mappings)

    Generate features

    Catel Caliburn.Micro Cinch (V2) MVVM light Simple MVVM WAFCommunicationwith other VMvia messenger

    Communicationwith other VMvia attributes

    RelayCommand/DelegateCommand

    EventToCommand trigger

    IoC containersupport

    Unity support

    MEF support

    Backgroundtask manager

    ViewModelManager with live VMinstances

    Project and itemtemplates

    Design timeexample datasupport

    Mementopattern

  • View model services

    Catel Caliburn.Micro Cinch (V2) MVVM light Simple MVVM WAFIMessage(Box)Service

    INavigationService

    IOpenFileService

    IPleaseWaitService

    IProcessService

    ISaveFileService

    IUIVisualizerservice

    Unit testversions ofservices

    Windows Phone specific view model services

    Catel Caliburn.Micro Cinch (V2) MVVM light Simple MVVM WAFIAccelerometerService

    ICameraService

    ICompassService

    IGyroscopeService

    ILocationService

    INavigationService

    IVibrateService

    Different interpretations of MVVMThere are two different interpretations of MVVM, the "purist" way where the model is protected, or the "shortcut" way where the view model onlyprovides the instance of the model and the view then binds directly to the model.

  • Shortcut interpretation

    This is what most people do. The view model once implements the model, and then provides the model to the view. The view then

    Advantages

    Easy to useFast since view model hardly contains any properties

    Disadvantages

    Always need to bind to Model.[PropertyName], but for view model properties it's just [PropertyName], might be confusingLess control over validation (you cannot insert logic between View Model where MVVM is all about

    Purist interpretation

    This is what the developers of Catel strongly believe in. It requires a bit more code, but gives great freedom and control and protection of themodel because all bindings go through the view model.

    Advantages

    Full contol and freedom, you can inject both logic and validation between view and model (what MVVM is actually about)Everything is available on the view model, no need for "sub-bindings" (such as Model.[PropertyName])Protection of your model from the view

    Disadvantages

    Needs a bit more code (but thanks to code snippets and the Expose attribute, this is not a big disadvantage)Validation in model or view model?I have had lots of discussion whether the validation should take place in the model or the view model. Some people think that the validationshould always occur inside the model because you dont want to persist invalid models to the persistence store. Others say that the models itselfdont need validation, but the state the view model is in requires the validation. I think both are true, and I will tell you why.

    First of all, you dont want invalid models in your persistence store. Thus, the most basic checks such as type, ranges and required fields shouldbe validated in the model. But sometimes, it is required to restrict the user more than the model does, and thats where validation in the viewmodel comes in handy. Another reason why you want to implement (a part of) the validation in the view model is the state of the model inside aworkflow. If you have a workflow that updates the model step by step, the model isnt valid after the first step in the workflow. However, you

  • already want to persist the model because the user might decide to execute the following steps at a later time. You dont want to implement thestate logic of a workflow in your model (and if you did that, get rid of it, as soon as possible). This is another feature where the view modelvalidation comes in handy.

    The good news is that with Catel, it doesnt matter what you want, because its all possible. If you want your model to do all the validation, thenthis is possible using the Model and ViewModelToModel attributes which map the values of the properties and the errors directly to the model sothe view model acts as a proxy between the view and the model. If you want to do all of the validation inside the view model, then you canimplement the ValidateFields and ValidateBusinessRules methods in the view model. And, if you want the best of both worlds, such as me, thanyou can use a combination of the techniques described above.

    Introduction to MVVM and modelsThis part of the documentation will explain all the parts of MVVM, in the order in which we think they must be built. First of all, the Models, whichare the closest to the business. Then, the View Models which define what part of the Models should be visible to the user in a specific situation.This also includes validation that is specific for the functionality that the View Model represents. Last, but not least, the View itself, which is thefinal representation of the View Model to the end-user.

    As you will notice (or maybe you don't), this framework has a lot in common with other MVVM frameworks out there. This is normal because all ofthe frameworks are trying to implement the same pattern, which isn't that hard to understand if you think about it long enough. Before we startedwriting the MVVM framework, we first investigated other frameworks because there already are enough, probably too many. However, even thebetter (or best) still took too much time to use, and there was too much code we had to write to create a View Model. That's the point where wedecided to write our own framework that contains lots of luxury for lazy developers such as us.

    ModelsThe Models part is one of the three major parts of MVVM. Therefore, I want to tell you a bit about what kind of Models we use to store our data.Basically, you can use all types of objects as Models, as long as the Models implement the most commonly used interfaces required by WPF orSilverlight.

    For MVVM, it is very important that the following interfaces are implemented:

    INotifyPropertyChangedIf this interface is not implemented, changes will not be reflected to the UI via bindings. In other words, your Model and View Model willbe useless in an MVVM setting.

    Finally, it is strongly recommended to have your Models implement the following interfaces as well:

    IDataErrorInfoIf this interface is not implemented, errors cannot be shown to the user.IEditableObjectIf this interface is not implemented, a Model cannot work with states. This means that a user cannot start editing an object and finallycancel it (because there is no stored state that can be used to restore the values).

    View ModelsView models are the classes that contain the view logic. They can be seen as a container for all models in a specific view, including the behaviorto interact with these models (for example adding or removing them from a collection). It is very important that a view model also implements the I

    interfaces just like the models do.NotifyPropertyChangedCreating view models with CatelThe View Models in Catel are very easy to write, and give the end-user a great flexibility in how to approach the Models. This part of the article willexplain the classes that make it possible to easily create View Models.

    The class is the most important class of all in the MVVM Framework of Catel. Of course, it can't do anything useful without theViewModelBaseother classes, but all the View Models that are created using Catel derive of this class. is based on the class thatViewModelBase ModelBaseships with Catel. Thanks to the existence of that class, the MVVM framework was set up very quickly (although very quickly is relative). Below isa class diagram that shows the class tree:

  • The class diagram above shows how many default interfaces of the .NET Framework are supported in the class. Since most of theseModelBaseinterfaces are used by WPF as well, the class itself can take huge advantage of the implementation of .ViewModelBase ModelBase

    Because derives from , you can declare properties exactly the same way. Even better, you can simply use ViewModelBase ModelBase ModelBas (or the extended ) to create (and save) your Models, and use as the base for all the View Models.e SavableModelBase ViewModelBase

    Creating a view modelTo declare a View Model, use the following code snippet:

    vm - defines a new view model

    When using the code snippet, this is the result:vm

  • /// /// $name$ view model./// public class $name$ViewModel : ViewModelBase{ #region Fields #endregion

    #region Constructors /// /// Initializes a new instance of the class. /// public $name$ViewModel () { } #endregion

    #region Properties /// /// Gets the title of the view model. /// /// The title. public override string Title { get { return "View model title"; } }

    // TODO: Register models with the vmpropmodel codesnippet // TODO: Register view model properties with the vmprop or vmpropviewmodeltomodelcodesnippets #endregion

    #region Commands // TODO: Register commands with the vmcommand or vmcommandwithcanexecutecodesnippets #endregion

    #region Methods #endregion}

    Declaring properties

    There are several code snippets available to create View Model properties:

    vmprop - Defines a simple View Model property.vmpropmodel - Defines a View Model property with . The property is also made private by default.ModelAttributevmpropviewmodeltomodel - Defines a View Model property with .ViewModelToModelAttribute

    When using the code snippet, this is the result:vmprop

    Note that declaring properties works exactly the same as declaring properties for the ModelBase

  • /// /// Gets or sets the name./// public string Name{ get { return GetValue(NameProperty); } set { SetValue(NameProperty, value); }}

    /// /// Register the Name property so it is known in the class./// public static readonly PropertyData NameProperty = RegisterProperty("Name",typeof(string));

    In the View, it is now possible to bind to the property of the View Model, as long as is set to an instance of the View Model.Name DataContext

    Declaring commandsThere are several code snippets available to create View Model commands:

    vmcommand - Defines a command that is always executable.vmcommandwithcanexecute - Defines a command that implements a method to determine whether the command can beCanExecuteinvoked on the View Model in its current state.

    When using the code snippet, this is the result:vmcommandwithcanexecute

    /// /// Gets the Add command./// public Command Add { get; private set; }

    // TODO: Move code below to constructorAdd = new Command(OnAddExecute, OnAddCanExecute);// TODO: Move code above to constructor

    /// /// Method to check whether the Add command can be executed./// /// The parameter of the command.private bool OnAddCanExecute(object parameter){ return true;}

    /// /// Method to invoke when the Add command is executed./// /// The parameter of the command.private void OnAddExecute(object parameter){ // TODO: Handle command logic here}

    The only thing left to do now is to move the creation of the command to the constructor (as the comments already instructs you to).In the View, it is now possible to bind any property (such as the property of a ) to the property of the View Model,Command Command Button Add

  • as long as DataContext is set to an instance of the View Model.

    Adding validationBecause the class derives from , it provides the same power of validation that the class has to offer. ViewModelBase ModelBase ModelBase Mode

    (and thus ) offers the following types of validation:lBase ViewModelBaseField warningsBusiness warningsField errorsBusiness errors

    ViewModelBase uses smart validation. This means that if the object is already validated, the object is not validated again to make sure that theView Models don't hit too much on the performance. Only when a property on the View Model changes, validation will be invoked. Of course, ifrequired, it is still possible to force validation when the View Model must be validated, even when no properties have changed.

    To implement field or business rule validation, you only have to override and/or the method:ValidateFields ValidateBusinessRules

    /// /// Validates the field values of this object. Override this method to enable/// validation of field values./// /// The validation results, add additional results tothis list.protected override void ValidateFields(List validationResults){ if (!string.IsNullOrEmpty(FirstName)) { validationResults.Add(FieldValidationResult.CreateError(FirstNameProperty,"First name cannot be empty")); }}

    /// /// Validates the field values of this object. Override this method to enable/// validation of field values./// /// The validation results, add additional results tothis list.protected override void ValidateBusinessRules(ListvalidationResults){ if (SomeBusinessErrorOccurs) { validationResults.Add(BusinessRuleValidationResult.CreateError("A businesserror occurred")); }}

    There are also other ways to add validation to a data object:Validation via data annotations - attributes such as the RequiredAttributeValidation via - custom validation such as IValidator FluentValidation

    The great thing is that Catel will gather all validation results from all different mappings and combine these into the . This contextValidationContextcan be used to query all sorts of validation info about an object.

    Interaction with models

    Note that it is also possible to re-use validation in a model using mappings or even external validation such as ModelToViewModel FluentValidation

  • One of the most important reasons why a View Model is created is because it serves as the glue between a View and the Model. Thecommunication between the View and the View Model is fully taken care of by WPF in the form of Bindings. The problem is that most of the time,a View Model is used to show a subset of a Model (which is, for example, a database entity).Most MVVM frameworks (actually, I haven't seen anyone not requiring manual updating) require manual updating, which brings us back to thestone age (remember the WinForms time setting the controls at startup, and reading the values at the end?). Catel solves this issue by providingconvenience attributes that take care of this dumb getting/setting story between the View Model and the Model. Catel fully supports getting/settingthe values from/to the Model, but believe me: you will love the attributes that are described next.

    ModelAttribute

    To be able to map values from/to a Model, it is important to know the actual Model. So, to let the View Model know what property represents theModel, can be used like shown below:ModelAttribute

    /// /// Gets or sets the person./// [Model]public Person Person{ get { return GetValue(PersonProperty); } private set { SetValue(PersonProperty, value); }}

    /// /// Register the Person property so it is known in the class./// public static readonly PropertyData PersonProperty = RegisterProperty("Person",typeof(Person));

    A Model setter is normally written as private (you normally don't want a UI to be able to change a Model), but the getter is public because youmight want to read info from it.

    Models in Catel are handled as very, very special objects. This means that as soon as a Model is set, Catel tries to call the IEditableObject.Begin method. Then, as soon as the Model is changed without being saved, or if the View Model is canceled, the Model is correctly canceled via Edit IE

    . If the Model is saved, the active Models will be committed via . I will leave the rest of the magicditableObject.CancelEdit IEditableObject.EndEditout of this article, but if you have any questions about it, don't hesitate to contact us!

    ViewModelToModelAttribute

    Now that we know how to declare a property has a Model, it is time to learn how we can communicate with it. Normally, you would have to watchthe Model to make sure it is synchronized correctly when the Model is updated. With Catel, this is not necessary any longer. Simply use ViewMod

    , and you will get the following advantages:elToModelAttribute

    Models are automatically being watched for changes, thus if a mapped property changes, the View Model is updated accordingly;When a View Model is changed, this property is automatically mapped to the Model;When the Model changes, the View Model is initialized automatically with the values of the new Model;When a Model has an error or warning (business or field), the warnings are mapped to the View Model so you can re-use the validationof the Model inside your View Model.

    So, you get all of this for free? No, you will have to decorate your property with , like shown below:ViewModelToModelAttribute

    Note that you should use the code snippet to create Model propertiesvmpropmodel

  • /// /// Gets or sets the first name./// [ViewModelToModel("Person")]public string FirstName{ get { return GetValue(FirstNameProperty); } set { SetValue(FirstNameProperty, value); }}

    /// /// Register the FirstName property so it is known in the class./// public static readonly PropertyData FirstNameProperty = RegisterProperty("FirstName",typeof(string));

    The code example is the easiest usage of the attribute that is available. It only provides the name of the Model property. This is required becauseit is possible (but not likely) to have multiple Models. But what if the property on your Model has a different name than your View Model? Noproblem, use the overload of the attribute as shown below:

    [ViewModelToModel("Person", "RealFirstName")]public string FirstName///... (remaining code left out for the sake of simplicity)

    The code above will map the property of the View Model to the property of the Person model.FirstName RealFirstName

    ExposeAttribute

    The is a great way to map properties between the model and the view model. However, sometimes the mappings areViewModelToModelAttributenot required for manual coding and should only be exposed from inside the view model to the view. The is great way to simplifyExposeAttributethis process. The code below is the same as declaring a model property named Person and 3 additional properties using the ViewModelToModel

    :Attribute

    /// /// Gets or sets the person./// [Model][Expose("FirstName")][Expose("MiddleName")][Expose("LastName")]private Person Person{ get { return GetValue(PersonProperty); } set { SetValue(PersonProperty, value); }}

    /// /// Register the Person property so it is known in the class./// public static readonly PropertyData PersonProperty = RegisterProperty("Person",typeof(Person));

    This feature is only available in WPF because Silverlight does not provide the or an equivalent featureICustomTypeDescriptor

  • Interaction with other view modelsNow that we've seen how easy it is to communicate between the View Model and the Model, you want more, right? I know how it is: You let 'emhave one finger, they take your whole hand. No worries, you can have my right hand, as long as I can keep my left one. Anyway, the developersof Catel are prepared for this. So, let's talk about the interaction with other View Models.

    Say, you have a multiple document interface (MDI as it was called in the old days). If you are following MVVM principles, every document (or tab)has its own View Model. Then, you want to be aware of updates of a single type of View Model. Say, for example, that there is a View Modelrepresenting a family called . This View Model is probably interested in changes in the .FamilyViewModel PersonViewModel

    ViewModelManager

    Let's start with the basics. As we have learned earlier in this article, all View Models created with the help of Catel derive from the ViewModelBaseclass. One of the things that this class does is that it registers itself with the class when it is being created, and it unregistersViewModelManageritself again when it is closed. So, simply said, is a class that holds a reference to all existing View Models at the moment.ViewModelManager

    InterestedInAttribute

    Now that we know about the class, and know that there is a repository that holds all of the live instances of all View ModelViewModelManagerclasses, it should be fairly easy to communicate with other View Models. It actually is; you just have to decorate a View Model with InterestedInAtt

    , as shown below:ribute

    [InterestedIn(typeof(FamilyViewModel))]public class PersonViewModel : ViewModelBase

    A View Model can have multiple instances, so it is possible to subscribe to multiple View Model types at the same time. OnceInterestedInAttributea View Model is decorated with , the View Model will receive all changes (and of course, the View Model that caused theInterestedInAttributechange) via the method, as shown below:OnViewModelPropertyChanged

    /// /// Called when a property has changed for a view model type that the current viewmodel is interested in. This can/// be accomplished by decorating the view model with the ./// /// The view model./// Name of the property.protected override void OnViewModelPropertyChanged(IViewModel viewModel, stringpropertyName){ // You can now do something with the changed property}

    MessageMediator

    Catel also offers a solution to the message mediator pattern in the form of the class. This is all described in the next sectionMessageMediator"Message mediator".Introduction to servicesServices are very important in MVVM. They define a way to interact with the user without using fixed controls such as or MessageBox SaveFileDi

    . The interfaces defined in Catel only define generic functionality of what to expect from a specific service. Using services is a great way toalogabstract away all specific functionality from a view model into a service that can be mocked during unit testing and can be used by other viewmodels as well.

    ServiceLocatorThe key behind services is the . The is the IoC (Inversion of Control) container that Catel provides. This is aServiceLocator ServiceLocator

    Note that it is also possible to get notified of commands that are being executed on other view models

  • container that contains all registrations and service instances available throughout the application. Retrieving services from the default ServiceLoc in Catel is very simple:ator

    var dependencyResolver = this.GetDependencyResolver();var messageService = dependencyResolver.Resolve();

    Dependency injectionA slightly better way to manage dependencies is to use dependency injection. The reason is that to instantiate a class, you always have toprovide all the dependencies. This way, all dependencies are always known to the caller which gives is a bit easier to control than having to knowwhat services are being used by a component (such as a view model).Catel fully supports dependency on view models. This means that a view model can have a constructor with several services. Catel willautomatically inject the services via the constructor. An example is below:

    public class PersonViewModel : ViewModelBase{ private readonly IMessageService _messageService; private readonly IPleaseWaitService _pleaseWaitService;

    public PersonViewModel(IMessageService messageService, IPleaseWaitServicepleaseWaitService) { _messageService = messageService; _pleaseWaitService = pleaseWaitService; }}

    Overview of servicesThe services below are available in Catel:

    Name Description

    IAccelerometerService Allows a developer to access the accelerometer of a Windows Phonedevice.

    ICameraService Allows a developer to use the PhotoCamera class in an MVVMmanner.

    ICompassService Allows a developer to access the compass of a Windows Phonedevice.

    IGyroscopeService Allows a developer to access the compass of a Windows Phonedevice.

    ILocationService Allows a developer to use GPS devices inside a view model.

    IMessageService Allows a developer to show message boxes from a view model.

    INavigationService Allows a developer to navigate to other pages inside an applicationusing view models only.

    IOpenFileService Allows a developer to let the user choose a file from inside a viewmodel.

    IPleaseWaitService