Click here to Skip to main content
15,895,799 members
Articles / Desktop Programming / WPF

MVVM made easy with Calcium - Part 1

Rate me:
Please Sign up or sign in to vote.
4.97/5 (39 votes)
18 Apr 2010BSD21 min read 123.2K   1.6K   109  
Learn how to create a simple MVVM pattern based application using the Calcium SDK.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

using DanielVaughan;
using DanielVaughan.Calcium;
using DanielVaughan.Calcium.CommandModel;
using DanielVaughan.Calcium.Gui;
using DanielVaughan.Calcium.Services;

using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Presentation.Commands;
using Microsoft.Practices.Composite.Presentation.Events;
using Meta = CalciumSample.ImageDesigner.Metadata.ImageDesignerViewModelMetadata;

namespace CalciumSample.ImageDesigner
{
    public class ImageDesignerViewModel : ViewModelBase
    {
        static CompositeCommand zoomInCompositeCommand = new CompositeCommand(true);
        public static CompositeCommand ZoomInCommand
        {
            get
            {
                return zoomInCompositeCommand;
            }
            set
            {
                zoomInCompositeCommand = value;
            }
        }

        readonly UICommand zoomInCommand;

        static CompositeCommand zoomOutCompositeCommand = new CompositeCommand(true);
        public static CompositeCommand ZoomOutCommand
        {
            get
            {
                return zoomOutCompositeCommand;
            }
            set
            {
                zoomOutCompositeCommand = value;
            }
        }

        readonly UICommand zoomOutCommand; 
 
        BitmapImage selectedImage;
        

        public BitmapImage SelectedImage
        {
            get
            {
                return selectedImage;
            }
            set
            {
                Notifier.Assign(Meta.SelectedImage, ref selectedImage, value);
            }
        }
        
        public ImageDesignerViewModel()
        {
            Title = "Image Designer";
            var eventAggregator = ServiceLocatorSingleton.Instance.GetInstance<IEventAggregator>();

            zoomInCommand = new UICommand(OnZoomInExecute, OnZoomInCanExecute);
            ZoomInCommand.RegisterCommand(zoomInCommand);

            ActiveChanged += delegate { zoomInCommand.IsActive = Active; };
            
            zoomOutCommand = new UICommand(OnZoomOutExecute, OnZoomOutCanExecute);
            ZoomOutCommand.RegisterCommand(zoomOutCommand);
            
            ActiveChanged += delegate { zoomOutCommand.IsActive = Active; };
            
            var clickImageEvent = eventAggregator.GetEvent<ClickImageEvent>();
            clickImageEvent.Subscribe(InsertImage, ThreadOption.UIThread);

            

            
            
        }

        bool OnZoomInCanExecute(object arg)
        {
            return true;
        }

        void OnZoomInExecute(object obj)
        {
            Scale += .1;
        }

        bool OnZoomOutCanExecute(object arg)
        {
            return true;
        }

        void OnZoomOutExecute(object obj)
        {
            Scale -= .1;
        }

        double scale = 1;

        public double Scale
        {
            get
            {
                return scale;
            }
            set
            {
                if (value < 0)
                {
                    return;
                }
                Assign("Scale", ref scale, value);
            }
        }


        void InsertImage(ClickImageEventArgs obj)
        {
            var viewService = ServiceLocatorSingleton.Instance.GetInstance<IViewService>();
			if (viewService.MainView == null || viewService.MainView.ViewModel != this)
			{
			    return;
			}
			
            SelectedImage = obj.BitmapImage;
        }
    }

    public class ClickImageEvent : CompositePresentationEvent<ClickImageEventArgs>
    {

    }

    public class ClickImageEventArgs
    {
        //public string FileName { get; set; }
        public BitmapImage BitmapImage { get; set; }
    }
}

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 BSD License


Written By
Product Manager Outcoder
United States United States
Katka has several years of experience working in software development in the areas of market research and e-commerce. She has wide ranging experience in developing Java, ASP.Net MVC, ASP.Net, WPF, Silverlight, and Windows Phone applications.

Company: Outcoder.com
Group: XAML Experts
Proud co-creator of: Surfy browser, Airlock Browser

Comments and Discussions