INotifyPropertyChanged Null Exception - Silverlight

Topics: Prism v2 - Silverlight 2
Feb 18, 2009 at 7:04 PM
Hi all, 

Great work on the framework!

I am receiving a Null Reference Exception when calling the INotifyPropertyChanged Event.  The property is definately not null, and it occurs while using the RegionContext functionality.  I have a region wherre the RegionContext property is bound to SelectedMediaItem property on my model.  When a selection is made and the SelectedMediaItem property is changed, everything works as expected.  I receive the SelectedMediaItem object from RegionContext.GetObservableContext().  However, once I set the dataContext of another frameworkelement to the SelectedMediaItem reference I received from the GetObservableContext method--I get a NullReferenceException on the SelectedMediaItem INotifyPropertyChanged event.  Specifically:

PARENT MODEL AND VIEW
AlbumViewModel and AlbumView
SelectedMediaItem is property of AlbumViewModel
IRegion (ContentControl) is bound to SelectedMediaItem, is located in AlbumView, and has a RegionContext bound to SelectedMediaItem

REGION CHILD
MediaItemDetailsViewModel and MediaItemDetailsView
in codebehind of this view I have the following code:

public

 

MediaItemDetailsView(IRegionManager regionManager) CONSTRUCTOR

 

{

 

this.regionManager = regionManager;

 

InitializeComponent();

 

RegionContext.GetObservableContext(this).PropertyChanged += this.RegionContextChanged;

 

}

 

void RegionContextChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)    EVENT HANDLER

 

{

 

 

var v = RegionContext.GetObservableContext(this).Value

 

 

this.DataContext = v;

 

}


I have a NotifyingObject base class that implements INotifyPropertyChanged from which all of my viewmodels inherit.  This is where the error is thrown.  Considering the exception information offers no help in debugging I thought to ask if anyone has seen this error and knows what causes it.

protected

 

virtual void OnPropertyChanged(string propertyName)

 

{

 

PropertyChangedEventHandler handler = PropertyChanged;

 

 

if (handler != null)

 

handler(

this, new PropertyChangedEventArgs(propertyName)); ERROR HAPPENS HERE

 

 

}

Thank you! --Lawrence King