Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caliburn Micro Guard Methods not evaluating on property change

I've been playing with the Caliburn Micro MVVM framework and am having some problems with guard methods.

I have a view model:

public class MyViewModel : PropertyChangedBase, IMyViewModel

A property:

public DateTime? Date
{
   get{return this.date; }
   set
   {
      this.date = value;
      this.NotifyOfPropertyChange(() => Date);
   }
}

Also, i have a method in my view model with a guard method

public void Calculate()
{
    // ..some code..
}

public bool CanCalculate()
{
    return this.Date.HasValue;
}

And a button in my view:

The problem I am having is that the CanCalculate method executes when loading but when I enter values into the text fields, it doesn't reevaluate the CanCalculate method. I am firing the property changed event on setting the databound view model properties so what could be the problem?

like image 638
Charlie Avatar asked Apr 05 '11 02:04

Charlie


2 Answers

Ok I figured it out. I didn't realise that you have to fire the guard method notification, thought the framework did that, but it makes sense.

So I change my property setter to:

public DateTime? Date
{
   get
   {
      return this.date; 
   }
   set
   {
      this.date = value;
      this.NotifyOfPropertyChange(() => Date);
      this.NotifyOfPropertyChange(() => CanCalculate);
   }
}

and changed my CanCalculate method to a property:

public bool CanCalculate
{
    get
    {
        return this.Date.HasValue;
    }
}

And all works fine now :)

like image 157
Charlie Avatar answered Sep 20 '22 09:09

Charlie


If you dont need CanExecute to be method, because you wont use parameters. Then you can rewrite it as property with standard notification and only getter. And call its PropertyChanged when you asume result of the getter changed.

like image 41
Euphoric Avatar answered Sep 22 '22 09:09

Euphoric