INotifyChanged or DependencyProperty when using 2+ classes

Topics: Prism v2 - WPF 3.5, Prism v2 - WPF 4
Jul 30, 2010 at 7:25 PM
Edited Jul 30, 2010 at 7:39 PM

I have used INotifiedChanged and DependencyProperties in the past and are OK comfortable with them, but they have also always been in the same class. Now I have a situation where I have one class that has a timer running that basically checks to see if a database is online every minute or so and sets a public property in another class to true or false. In my viewmodel I would like to use that public property to set the boolean for my Delegate commands CanExecute. However even though the public property gets changed the viewmodels CanExecute never knows about it even though I call RaisePropertyChanged on it.

I know this has to be something simple that I just can't see, but all my Google attempts have left me still in limbo. Below are snippets from the two classes who need to talk. They are both in the same project and both implement INotifyPropertyChanged. I was thinking that maybe IEventAggregator needs ot be used here, but I usually save that bad boy for when I need to do Module to Module communication.

Any help in explaining on how to accomplish this would be greatly appreciated. 


DBSynchronization Class (Contains the public bool property) 

private bool isDatabaseOnline = false;
        public bool IsDatabaseOnline
            get { return isDatabaseOnline; }
                isDatabaseOnline = value;


        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string prop)
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(prop));


ViewModel class that needs to be notified when it changes


 bool CanSyncExecute()
            if (dbSync.IsDatabaseOnline)
                RibbonBarFactory.GetGroupByName(ModuleController.ModuleTab,Resources.DatabaseRibbonGroupHeader).Header = Resources.DatabaseRibbonGroupHeader + " (Online)";
                RibbonBarFactory.GetGroupByName(ModuleController.ModuleTab,Resources.DatabaseRibbonGroupHeader).Header = Resources.DatabaseRibbonGroupHeader + " (Offline)";




Jul 30, 2010 at 10:48 PM


Based on our understanding, you need to update the value that indicates whether a command can be executed in a cross-module scenario.

That said, in the implementation you shared, all appears to be correct, except by in the ViewModel there is not a subscription to the PropertChangedEvent raised from your DBSynchronization class. So, it might be the reason why your ViewModel is not notified.

Important: when using DelegateCommands, you must manually call to its RaiseCanExecuteChanged method to get the UI updated.

On the other hand and as you mentioned, I think you could avoid implementing INotifyPropertyChanged in the DBSynchronization class and use EventAggregator instead. This way, you could publish this event when IsDatabaseOnline property changes and additionally you would obtain a more decoupled application. 

In addition, you could take a look at the Event Aggregation Quickstart.

Please let me know if this helps.

Fernando Antivero

Jul 31, 2010 at 12:50 AM


Subscribing to the event was exactly what I neeed.

This code does the magic inside the viewmodel.

//Subscribe to DBSync change events
            dbSync.PropertyChanged += new PropertyChangedEventHandler(dbSync_PropertyChanged);

Thank you for the response!