I am trying to disable an XAML button until the form is valid, at initial load it is disabled but not enabled on the model validation pass.
Here is my code
public class LoginRequest
{
public string Email { get; set; }
public string Password { get; set; }
}
public sealed partial class LoginViewModel : ObservableObject
{
[ObservableProperty] [NotifyCanExecuteChangedFor(nameof(LoginCommand))]
private LoginRequest _form;
bool IsFormValid()
{
return !string.IsNullOrEmpty(Form.Email) && !string.IsNullOrEmpty(Form.Password);
}
[RelayCommand(CanExecute = nameof(IsFormValid))]
async Task Login()
{
//...
}
}
xmlns:vm="clr-namespace:MyProject.ViewModels.Account"
x:Class="MyProject.Pages.Account.LoginPage"
x:TypeArguments="vm:LoginViewModel"
x:DataType="vm:LoginViewModel">
<Entry Placeholder="Email"
Text="{x:Binding Form.Email, Mode=TwoWay}">
</Entry>
<Entry Placeholder="Password"
Text="{x:Binding Form.Password, Mode=TwoWay}">
</Entry>
<Button Text="Login" Command="{Binding LoginCommand}">
</Button>
When I add individual properties at my ViewModel its working as expected but to an object.
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(LoginCommand))]
private string email;
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(LoginCommand))]
private string password;
What am I missing here?
Your code only modifies the Property of the Form instance instead of changing the value of Form itself.
So, if you want to get notified when a Property of an Object changes, you may try the following way,
First, also make the
EmailandPasswordto be anObservableProperty.Second, ObservableObject has a PropertyChanged EventHandler, which will occur when a Property has changed. So in your viewmodel, register and implement the PropertyChanged event handler for the Form instance.
Hope it helps!