Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Catel - Part 3 of n: The MVVM Framework

, 28 Jan 2011
This article explains the MVVM framework that ships with Catel.
catel-03_01-mvvm-framework.zip
Examples
BasicViewModel
DataWindow
ControlToViewModelWithWindow
ControlToViewModelWithControl
catel-03_01-mvvmframework.zip
using Catel.Articles._03___MVVM.Models;
using Catel.Articles.Base.Data.Attributes;
using Catel.Data;
using Catel.Windows.MVVM;

namespace Catel.Articles._03___MVVM.Examples.BasicViewModel
{
    /// <summary>
    /// BasicViewModel view model.
    /// </summary>
    [Example("Basic view model", "Shows how to create your first view model",
        LongDescription = "Shows the result of the usage of the vm (for the view model), vmpropmodel (for the model), " +
                          "vmpropviewmodeltomodel (view model to model mapping) and vmcommand (command) code snippets.",
        SourceFileName = "/Examples/BasicViewModel/PersonViewModel.cs")]
    public class PersonViewModel : ViewModelBase
    {
        #region Constructor & destructor
        /// <summary>
        /// Initializes a new instance of the <see cref="PersonViewModel"/> class.
        /// </summary>
        /// <param name="person">The person.</param>
        public PersonViewModel(Person person)
            : base()
        {
            // Store values
            Person = person;

            // Create commands
            CreateChildren = new Command<object>(CreateChildren_Execute);
        }
        #endregion

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

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

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

        #region View model
        /// <summary>
        /// Gets or sets the gender.
        /// </summary>
        [ViewModelToModel("Person")]
        public Gender Gender
        {
            get { return GetValue<Gender>(GenderProperty); }
            set { SetValue(GenderProperty, value); }
        }

        /// <summary>
        /// Register the Gender property so it is known in the class.
        /// </summary>
        public static readonly PropertyData GenderProperty = RegisterProperty("Gender", typeof(Gender));

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

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

        /// <summary>
        /// Gets or sets the middle name.
        /// </summary>
        [ViewModelToModel("Person")]
        public string MiddleName
        {
            get { return GetValue<string>(MiddleNameProperty); }
            set { SetValue(MiddleNameProperty, value); }
        }

        /// <summary>
        /// Register the MiddleName property so it is known in the class.
        /// </summary>
        public static readonly PropertyData MiddleNameProperty = RegisterProperty("MiddleName", typeof(string));

        /// <summary>
        /// Gets or sets the last name.
        /// </summary>
        [ViewModelToModel("Person")]
        public string LastName
        {
            get { return GetValue<string>(LastNameProperty); }
            set { SetValue(LastNameProperty, value); }
        }

        /// <summary>
        /// Register the LastName property so it is known in the class.
        /// </summary>
        public static readonly PropertyData LastNameProperty = RegisterProperty("LastName", typeof(string));
        #endregion
        #endregion

        #region Commands
        /// <summary>
        /// Gets the CreateChildren command.
        /// </summary>
        public Command<object> CreateChildren { get; private set; }

        /// <summary>
        /// Method to invoke when the CreateChildren command is executed.
        /// </summary>
        /// <param name="parameter">The parameter of the command.</param>
        private void CreateChildren_Execute(object parameter)
        {
            // TODO: Handle command logic here
        }
        #endregion

        #region Methods
        /// <summary>
        /// Initializes the object by setting default values.
        /// </summary>	
        protected override void Initialize()
        {
            // TODO: Implement logic to initialize the view model
        }

        /// <summary>
        /// Validates the fields.
        /// </summary>
        protected override void ValidateFields()
        {
            // TODO: Implement any field validation of this object. Simply set any error by using the SetFieldError method
        }

        /// <summary>
        /// Validates the business rules.
        /// </summary>
        protected override void ValidateBusinessRules()
        {
            // TODO: Implement any business rules of this object. Simply set any error by using the SetBusinessRuleError method
        }

        /// <summary>
        /// Saves the data.
        /// </summary>
        /// <returns>
        /// 	<c>true</c> if successful; otherwise <c>false</c>.
        /// </returns>	
        protected override bool Save()
        {
            // TODO: Implement logic when the view model is saved
            return true;
        }
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Geert van Horrik
Software Developer CatenaLogic
Netherlands Netherlands
I am Geert van Horrik, and I have studied Computer Science in the Netherlands.
 
I love to write software using .NET (especially the combination of WPF and C#). I am also the lead developer of Catel, an open-source application development framework for WPF, Silverlight, WP7 and WinRT with the focus on MVVM.
 
I have my own company since January 1st 2007, called CatenaLogic. This company develops commercial and non-commercial software.
 
To download (or buy) applications I have written, visit my website: http://www.catenalogic.com
Follow on   Twitter

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 28 Jan 2011
Article Copyright 2010 by Geert van Horrik
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid