How do 'Can' guard methods (like CanSayHello) get called?

Topics: Conventions, Getting Started
Jul 5, 2012 at 12:14 AM

I've got an ObservableCollection displayed in a WPF grid. The form has a 'Save' button which I want to enable when data in the grid has changed, either because the underlying data changed or because the user changed the grid contents.

public bool CanButtonSave
   // return true if ButtonSave should be enabled

public void ButtonSave()
   // do save stuff here

What I'm missing is how to cause the CanButtonSave event to fire when the data changes. Help, anyone?


Jul 5, 2012 at 9:12 AM

If the CanButtonSave is a property, you need to raise a property changed event.

If the guard is a method, you have no way to rise such notification and you must rely on the WPF framework to call your function guard when it requires it.

Jul 5, 2012 at 4:18 PM

Thanks, BladeWise.

So when the user changes the contents of a cell, do I have to handle an event that fires when the data changes and call NotifyOfPropertyChange from that handler? Or is there some simpler way to do this?

Jul 5, 2012 at 5:37 PM

You can write up an helper method, but basically, it is up to you to notify about a guard change, since there is no way for CM to understand when a guard requires re-evaluation.