Calling [AlsoNotifyCanExecuteFor] on a Get Only Property for an ICommand #185
-
I have an ICommand that needs a CanExecute attached to it. The CanExecute is triggered by a get only property that is updated elsewhere using [AlsoNotifyChangeFor(nameof(SomeBool)]. I can't attach [AlsoNotifyCanExecuteFor] to my get only property since it's not a field. What is the correct way to go about this? I really enjoy the code generators and would like to not have to write out the relay command if I don't have to. [ObservableProperty]
[AlsoNotifyChangeFor(nameof(SomeBool))]
private string someString;
[AlsoNotifyCanExecuteFor(nameof(SomeFunctionCommand))] // <-- point of failure
public bool SomeBool => someClass.Status;
[ICommand]
private void SomeFunction()
{
// allowed to do stuff iff SomeBool is true
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
In this case you'd need to just move |
Beta Was this translation helpful? Give feedback.
-
Hi @Sergio0694, can you give me an example of
I don't quite understand it. I'm sure I used it wrong [ICommand]
private void SomeFunction()
{
CanExecute = nameof(SomeBool); // doesn't compile.
// allowed to do stuff iff SomeBool is true
} |
Beta Was this translation helpful? Give feedback.
-
I implemented as suggested, and now I get a reflection error.
Full example
|
Beta Was this translation helpful? Give feedback.
In this case you'd need to just move
[AlsoNotifyCanExecuteFor(nameof(SomeFunctionCommand)]
oversomeString
. Also, note that you will also need to setCanExecute = nameof(SomeBool)
in your command, otherwise it won't actually use the value of that property to decide whether it can be executed 🙂