mariocatch
mariocatch

Reputation: 8693

Stylet Button not enabling when its CanX property returns true

Using Stylet

I'm using the default Stylet template with dotnet core 3.1. I simply added a property/method for reproducing the issue.

<TextBox Text="{Binding Username, 
                Mode=TwoWay, 
                UpdateSourceTrigger=PropertyChanged}" 
         Width="100" Height="100"/>

<Button Command="{s:Action Do}">Foo</Button>
public class ShellViewModel : Screen
{
    public string Username { get; set; }

    public void Do() { }

    public bool CanDo => !string.IsNullOrWhiteSpace(Username);
}

Breakpoint on CanDo property is hit once and never hit again. Changing the value in the TextBox, while calling set, does not retrigger the CanDo property.

I've also tried using backing fields with SetAndNotify, to no avail:

public string Username { get => username; set => SetAndNotify(ref username, value, nameof(Username)); }

Upvotes: -1

Views: 72

Answers (2)

LegoCoder
LegoCoder

Reputation: 19

PropertyChangedBase also takes care to integrate with Fody.PropertyChanged. Notifications raised by Fody.PropertyChanged are raised using the PropertyChangedDispatcher.

Therefore you do not need to do anything special in order to use Fody.PropertyChanged with any subclass of Screen, ValidatingModelBase, or PropertyChangedBase.

This is from the Stylet Document wiki. If a project is using Fody.PropertyChanged, then it don't have to write the NotifyOfPropertyChange() manually, maybe that's why you didn't see NotifyOfPropertyChange() in the sample codes.

Fody.PropertyChanged is a wonderful package, try it, it saves me millions of keystrokes :)

Upvotes: -1

mariocatch
mariocatch

Reputation: 8693

I realize I am missing property changed notifications. Through all of the examples I saw I assumed this was handled implicitly by Stylet for me.

After changing my property setters to the following, it works as expected.

public string Username
{
    get => _username;
    set
    {
        SetAndNotify(ref _username, value);
        NotifyOfPropertyChange(() => CanLogin);
    }
}

Upvotes: 0

Related Questions