C# .NET - Implementing the INotifyPropertyChanged Interface on the MonthViewCalendar Class

Asked By Michael Collins on 12-Sep-14 10:27 AM
First off, the code that I refer to is from here:
http://www.nullskull.com/a/1382/styling-the-wpf-calendar-to-resemble-outlooks-month-view-calendar.aspx

I went through the extremely useful walk-through to create an Outlook styled calendar. The issue that I am having is implementing the last part of the tutorial... The author mentions in passing, "This can be fixed by implementing the INotifyPropertyChanged interface on the MonthViewCalendar class and raising the PropertyChanged event when an appointment is added." This is in reference to having the list boxes update.
I am very new to WPF and so I admit that I have almost no idea what this means, but would like to use this piece of code in an application that I am developing.
Robbe Morris replied to Michael Collins on 12-Sep-14 04:42 PM
Databinding in WPF/XAML is all about reacting to objects raising events on themselves when data changes.  It is what allows the user interface controls to update themselves without you having to write code to redraw a row, treenode, or perhaps the entire control.

Via databinding in XAML, you essentially tell the control to subscribe to the PropertyChanged event on the individual object property (the syntax does this on its own).  Then, inside the data object, you have to include code to raise the PropertyChanged event inside the property "set" portion of the get/set of the property.  Using something like an ObservableCollection as your data store takes care of raising events when the contents of the collection change but it does not address data object property value changes on items in the ObservableCollection. 

The easiest way to implement this is to create a sort of DataObjectBase class that contains all of the required aspects of implementing INotifyPropertyChanged.  There isn't all whole lot that is required.

Then you have all applicable data object classes inherit DataObjectBase.  Something like this:

public class ArticleAdmin : MyNamespace.DataObjectBase, INotifyPropertyChanged

Your property would look like this:

private Guid _userID = System.Guid.Empty;

public Guid UserID
{
  get
  {
    return _userID;
  }
  set
  {
    if (value != _userID)
    {
    _userID = value;
    PropertyChangedHandler(this, "UserID");  // method in DataObjectBase to raise event.
    }
  }
}

 

There is a good example of how to implement it on MSDN:

http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx
Michael Collins replied to Robbe Morris on 15-Sep-14 01:33 PM
Hi Robbe:

I am working on implementing this now. Thanks in advance for the detailed explanation! If I have any other questions, I will let you know.

Thanks again,
MTC