canton7 / Stylet

A very lightweight but powerful ViewModel-First MVVM framework for WPF for .NET Framework and .NET Core, inspired by Caliburn.Micro.
MIT License
977 stars 143 forks source link

Conditional methods with parameters as actions? #147

Open vgist1 opened 3 years ago

vgist1 commented 3 years ago

Hi @canton7,

Thanks for this awesome library, I have been using it and I just love how simple and powerful it is.

On caliburn you can use methods with parameters for conditions(for reference: https://caliburnmicro.com/documentation/actions#action-parameters), say

public bool CanDoSomething(string name)
{
    return !string.IsNullOrWhiteSpace(name);
}

public void DoSomething(string name)
{
    // do something
}

From what I read on the documentation with Stylet that is not possible with a method or taking a parameter.

I could probably do a hacky approach which would involve binding 2 properties to achieve something close to the above but its no ideal.

Do you have any plans on adding that into Stylet or have I missed something and that is indeed possible?

canton7 commented 3 years ago

Looks like they added guard methods since I last used it.

The tricky bit here is knowing when the re-evaluate the guard method. With guard properties we use INPC, but that doesn't work for methods. It looks like Caliburn.Micro used cal:Parameter, which adds a bunch of additional complexity.

If you can come up with a nice way of implementing something similar to this, I'm all ears. This might also be useful

canton7 commented 3 years ago

Actually, if the button (or whatever) evaluates ICommand.CanExecuted whenever its CommandParameter property changes, that could work. I'll have to experiment.

canton7 commented 3 years ago

Ah no, doesn't look like that works, sadly.

vgist1 commented 3 years ago

Looks like they added guard methods since I last used it.

The tricky bit here is knowing when the re-evaluate the guard method. With guard properties we use INPC, but that doesn't work for methods. It looks like Caliburn.Micro used cal:Parameter, which adds a bunch of additional complexity.

If you can come up with a nice way of implementing something similar to this, I'm all ears. This might also be useful

I would do that if I had the knowledge but I am just a beginner and I am afraid that would be too much for me to do at this moment.

I am a student and am just learning the principles of mvvm, wpf, etc.

But thank you very much for taking the time and answer.

WillBellJr commented 3 years ago

I agree, thanks Canton7 for taking the time to look into this suggestion.

I'm not new to MVVM, but always struggled with what I was using (mostly DevExpress MVVM) - I've been successfully using Stylet_ now for a couple of projects, AND I have to say, Stylet has finally made using MVVM with my WPF apps so much easier.

I look forward to ANY additions to this project!

canton7 commented 3 years ago

Heh, maybe Action can subscribe to changes on the button's CommandParameter. That would be a very hacky but probably quite neat way of doing this.