Implementing Model-View-ViewModel in Silverlight

Click here to play this video
Posted by craigshoemaker
02-25-2009


Downloads: 2,079
File size: 168.8MB
Views: 10,346

Join Tim Heuer and Craig Shoemaker to learn to build a simple quiz application using Model-View-ViewModel in Silverlight.

Download code sample: mvvm-sl.zip

Duration: 1 hour

Download

  • Full: 1024x768, 181MB
  • Web: 640x480, 168MB
  • Mobile: 320x240, 90MB

Comments

rubans wrote re: Implementing Model-View-ViewModel in Silverlight
on 09-29-2009 11:27 AM

It doesn't seem to work for me when Click on the submit button, get a Object reference not set to an instance of an object.

venkatpatta wrote re: Implementing Model-View-ViewModel in Silverlight
on 10-28-2009 11:19 AM

@rubans: Button click code is trying to set data to ViewModel instead of ViewModel.Questions. Changed as below to get it working.

       private void Button_Click(object sender, RoutedEventArgs e)

       {

           //var data = this.DataContext as QuestionViewModel;

           //data.SubmitAnswers();

           QuestionViewModel vminst = new QuestionViewModel();

           vminst.Questions = this.DataContext as ObservableCollection<PMPSample.Model.Question>;

           vminst.SubmitAnswers();

       }

Mev wrote re: Implementing Model-View-ViewModel in Silverlight
on 11-24-2009 10:47 AM

That didn't work either.

Changed as follows to make it work:

       private void Button_Click(object sender, RoutedEventArgs e)

       {

           ObservableCollection<PMPSample.Model.Question> q = this.DataContext as ObservableCollection<PMPSample.Model.Question>;

           QuestionViewModel vminst = new QuestionViewModel();

           vminst.Questions = q;

           vminst.SubmitAnswers();

       }

Namespace added System.Collections.ObjectModel

Todd Miranda wrote re: Implementing Model-View-ViewModel in Silverlight
on 12-04-2009 11:42 PM

Actually, there are a few changes that should be made.  You really don't want to create a new instance of the QuestionViewModel in your view.  So the original code in the button click event works if you make a different change. You really want to set the DataContext differently.

void Page_Loaded(object sender, RoutedEventArgs e)

{

  QuestionViewModel qdata = new QuestionViewModel();

  qdata.FetchQuestions();

  QuestionDataView.DataContext = qdata;

}

Notice the change to set the DataContext to the actual View.  Now change the View's XAML to set the ItemsSource property of the ListView.

<ItemsControl ItemsSource="{Binding Questions}">

This way, you have bound the actual ViewModel to the DataContext of your View and you can access any of the properties of your ViewModel in the View.

Hopefully that makes sense.

rpallas wrote re: Implementing Model-View-ViewModel in Silverlight
on 01-24-2010 4:48 PM

@Todd:

When i change the DataContext of the view to the viewmodel object, the exception no longer fires when i click the button but the questions no longer appear in the ItemsControl. All that gets displayed is the button.

Do i need to change the bindings for the UI elements or should this just work?

Thanks

Robbie

rpallas wrote re: Implementing Model-View-ViewModel in Silverlight
on 01-24-2010 5:13 PM

Sorry, this was my mistake. I had created the Questions ObservableCollection as a field instead of a property.

I.e. public ObservableCollection<Question> Questions;

instead of:

public ObservableCollection<Question> Questions { get; set; }

Took me a little while to find :)

Thanks

Robbie

Add a Comment


Sign in to post a comment.