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?
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 :)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With