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

Topics: Conventions, Getting Started
Jul 4, 2012 at 11:14 PM

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 8: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 3: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 4: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.