Friday, 15 June 2012

WPF MVVM Code Sample

This is simple WPF Application using MVVM design patterns.following code shows how we can design WCF application.

Step(1) First create xaml file



<Window x:Class="WPF_MVVM.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
 
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Label Grid.Column="0" Grid.Row="0" Content="Example 2 - this works!" />
        <Label Grid.Column="0" Grid.Row="1" Content="Artist:  " />
        <Label Grid.Column="1" Grid.Row="1" Content="{Binding ArtistName}" />
        <Button Grid.Column="1" Grid.Row="2" Name="ButtonUpdateArtist"
        Content="Update Artist Name" Click="ButtonUpdateArtist_Click" />
    </Grid>

</Window>



Step(2) code behind file will be as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPF_MVVM
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        #region Members
        SongViewModel _viewModel;
        int _count = 0;
        #endregion



        public Window1()
        {
            SongViewModel _viewModel = new SongViewModel();

            InitializeComponent();
            base.DataContext = _viewModel;

            //  We have declared the view model instance declaratively in the xaml.
            //  Get the reference to it here, so we can use it in the button click event.
            //_viewModel = (SongViewModel)base.DataContext;


        }

        private void ButtonUpdateArtist_Click(object sender, RoutedEventArgs e)
        {
            ++_count;
            _viewModel = (SongViewModel)base.DataContext;
            _viewModel.ArtistName = string.Format("Artist ({0})", _count);
        }

    }
}


Step(3)Create song.cs class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WPF_MVVM
{
    class Song
    {
        #region Members
        string _artistName;
        string _songTitle;
        #endregion

        #region Properties
        /// The artist name.
        public string ArtistName
        {
            get { return _artistName; }
            set { _artistName = value; }
        }

        /// The song title.
        public string SongTitle
        {
            get { return _songTitle; }
            set { _songTitle = value; }
        }
        #endregion

    }
}

Step(4) Create Model classs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace WPF_MVVM
{
    //class SongViewModel
    //{
    //    Song _song;

    //    public Song Song
    //    {
    //        get
    //        {
    //            return _song;
    //        }
    //        set
    //        {
    //            _song = value;
    //        }
    //    }

    //    public string ArtistName
    //    {
    //        get { return Song.ArtistName; }
    //        set { Song.ArtistName = value; }
    //    }

    //}



    public class SongViewModel : INotifyPropertyChanged
    {
        #region Construction
        /// Constructs the default instance of a SongViewModel
        public SongViewModel()
        {
            _song = new Song { ArtistName = "Shekhar", SongTitle = "RHTDM" };
        }
        #endregion

        #region Members
        Song _song;

        #endregion

        #region Properties
         Song Song
        {
            get
            {
                return _song;
            }
            set
            {
                _song = value;
            }
        }
        public string ArtistName
        {
            get { return Song.ArtistName; }
            set
            {
                if (Song.ArtistName != value)
                {
                    Song.ArtistName = value;
                    RaisePropertyChanged("ArtistName");
                }
            }
        }
        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Methods

        private void RaisePropertyChanged(string propertyName)
        {
            // take a copy to prevent thread issues
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }


}

Step(5) Application Output will be as follows:




No comments:

Post a Comment