Click here to Skip to main content
Click here to Skip to main content

Silverlight View Model Communication

By , 25 Sep 2010
 

Live example: [click here]

Also see:

Simple View Model Communication

With using "View Model Style" programming, it may be a little confusing as to how you can communicate between View Models. There are various methods available, but this article explores what I hope to be an easy simple method.

View Model Style

View Model Style allows a programmer to create an application that has absolutely no UI (user interface). The programmer only creates a View Model and a Model. A designer with no programming ability at all, is then able to start with a blank page and completely create the View (UI) in Microsoft Expression Blend 4 (or higher). If you are new to View Model Style, it is suggested that you read Silverlight View Model Style : An (Overly) Simplified Explanation for an introduction.

The Child Application

First, we will create a Silverlight Application that will be the Child Application. We use the following code for the View Model:

public class ChildVMModel : INotifyPropertyChanged
{
    public ChildVMModel()
    {
        AddNewNameCommand = new DelegateCommand(AddNewName, CanAddNewName);
        DeleteNameCommand = new DelegateCommand(DeleteName, CanDeleteName);
    }
    
    #region AddNewNameCommand
    public ICommand AddNewNameCommand { get; set; }
    public void AddNewName(object param)
    {
        // Get the name
        string strName = (String)param;
        // Add the name to the collection
        colNames.Add(strName);
        
        // Update Number of Names
        NumberOfNames = colNames.Count;
    }
    
    private bool CanAddNewName(object param)
    {
        return true;
    }
    #endregion
    
    #region DeleteNameCommand
    public ICommand DeleteNameCommand { get; set; }
    public void DeleteName(object param)
    {
        // Get the name
        string strName = (String)param;
        // Remove the name
        colNames.Remove(strName);
        
        // Update Number of Names
        NumberOfNames = colNames.Count;
    }
    
    private bool CanDeleteName(object param)
    {
        return true;
    }
    #endregion
    
    // Properties
    
    #region NumberOfNames
    private int _NumberOfNames = 0;
    public int NumberOfNames
    {
        get { return _NumberOfNames; }
        private set
        {
            if (NumberOfNames == value)
            {
                return;
            }
            _NumberOfNames = value;
            this.NotifyPropertyChanged("NumberOfNames");
        }
    }
    #endregion
    
    // Collections
    
    #region colNames
    private ObservableCollection<String> _colNames
        = new ObservableCollection<String>();
    public ObservableCollection<String> colNames
    {
        get { return _colNames; }
        private set
        {
            if (colNames == value)
            {
                return;
            }
            _colNames = value;
            this.NotifyPropertyChanged("colNames");
        }
    }
    #endregion
    
    #region INotifyPropertyChanged
    public event PropertyChangedEventHandler PropertyChanged;
    
    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
    #endregion
}

We then create a View and bind the Properties, Collections, and ICommands to the View Model.

We then create a MainPage View (that has its own View Model), and in the Assets > Project, we grab the View and drop it on the page.

We can add and delete items to the list. The count of the items will also show.

The Master View

If we want to communicate with the Child View from the MainPage View, we can use the following method.

First, we add this code to the MainPage View Model:

private ChildVMModel _objChildVMModel = new ChildVMModel();
public ChildVMModel objChildVMModel
{
    get { return _objChildVMModel; }
    private set
    {
        if (objChildVMModel == value)
        {
            return;
        }
        _objChildVMModel = value;
        this.NotifyPropertyChanged("objChildVMModel");
    }
}

This provides a property that we can bind the Child View to.

When we build the project and look at the Data Context, we see the Child View Model (objChildVMModel).

Bind the Child View Model to The MainPage View Model

On the MainPage View, select the Child View from the Objects and Timeline window.

Select Advanced options under Properties.

Select Data Binding...

Select the Child View Model.

Next, add controls to the MainPage View.

We then bind the Properties, Collections, and ICommands.

For example, we can drop a behavior on the Add button...

...and easily bind to the ICommand in the Child View Model.

The MainPage View and the Child View will now communicate.

License

This article, along with any associated source code and files, is licensed under The MIT License

About the Author

defwebserver
Software Developer (Senior) http://ADefWebserver.com
United States United States
Member
Michael Washington is a Microsoft Silverlight MVP. He is a Silverlight developer and an ASP.NET, C#, and Visual Basic programmer.
 
He is a DotNetNuke Core member and has been involved with DotNetNuke for over 4 years. He is the Co-Author of Building Websites with DotNetNuke (4 and 5).
 
He is one of the founding members of The Open Light Group (http://openlightgroup.net).
 
He is the founder of http://LightSwitchHelpWebsite.com
 
He has a son, Zachary and resides in Los Angeles with his wife Valerie.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionno sibling communication.memberkernel salonpas29 Jun '11 - 17:24 
AnswerRe: no sibling communication.mvpdefwebserver29 Jun '11 - 18:03 
Generalgood article, but...memberpowerdude24 May '11 - 12:22 
GeneralRe: good article, but...mvpdefwebserver24 May '11 - 12:25 
GeneralRe: good article, but...mvpdefwebserver24 May '11 - 12:27 
GeneralRe: good article, but...membersteven carney15 Feb '12 - 23:28 
GeneralMy vote of 5memberRichard Waddell25 Sep '10 - 14:09 
GeneralRe: My vote of 5memberdefwebserver25 Sep '10 - 15:12 
GeneralMy vote of 5memberKiran Kumar G17 Aug '10 - 23:26 
GeneralRe: My vote of 5memberdefwebserver25 Sep '10 - 11:31 
GeneralMy vote of 5membermburnie13 Jul '10 - 0:02 
GeneralRe: My vote of 5memberdefwebserver14 Jul '10 - 4:06 
GeneralLooks good, & solves a problem I never knew existed!memberAlan Beasley4 Jul '10 - 23:03 
GeneralRe: Looks good, & solves a problem I never knew existed!memberdefwebserver5 Jul '10 - 12:43 
GeneralGroundbreaking - answers an important question I hadn't thought to ask yetmemberMember 24098344 Jul '10 - 8:39 
GeneralRe: Groundbreaking - answers an important question I hadn't thought to ask yetmemberdefwebserver4 Jul '10 - 8:46 
GeneralRe: Groundbreaking - answers an important question I hadn't thought to ask yetmemberdefwebserver25 Sep '10 - 11:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 25 Sep 2010
Article Copyright 2010 by defwebserver
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid