C
C#7mo ago
eduardoA

Microsoft MVVM toolkit

I have a very simple question I have two entries and a button
<inputLayout:SfTextInputLayout
ContainerBackground="White"
ContainerType="Outlined"
Hint="Email"
IsHintAlwaysFloated="True"
OutlineCornerRadius="8">
<Entry />
</inputLayout:SfTextInputLayout>

<inputLayout:SfTextInputLayout
Grid.Row="1"
ContainerBackground="White"
ContainerType="Outlined"
Hint="Password"
IsHintAlwaysFloated="True"
OutlineCornerRadius="8">
<Entry />
</inputLayout:SfTextInputLayout>

this is the command I am executing
<inputLayout:SfTextInputLayout
ContainerBackground="White"
ContainerType="Outlined"
Hint="Email"
IsHintAlwaysFloated="True"
OutlineCornerRadius="8">
<Entry />
</inputLayout:SfTextInputLayout>

<inputLayout:SfTextInputLayout
Grid.Row="1"
ContainerBackground="White"
ContainerType="Outlined"
Hint="Password"
IsHintAlwaysFloated="True"
OutlineCornerRadius="8">
<Entry />
</inputLayout:SfTextInputLayout>

this is the command I am executing
[RelayCommand] async Task Register() { IsPopOpen = false; //await dataService.AddAsync("Users", Student); } ``` what I want to do is enable the button only if the email and password are entered
5 Replies
Petris
Petris7mo ago
@ref *&Sergio this is the toolkit of yours, right?
Sergio
Sergio7mo ago
Yeah You'll want to define some bool CanRegister property or method in your viewmodel. Make that check that the password and email are not empty. Make those two observable properties and have them notify the command. Then make the command have CanExecute = nameof(CanRegister) See docs here
eduardoA
eduardoA7mo ago
thanks can you help me @ref *&Sergio
[ObservableProperty]
bool isVisible = true;

[ObservableProperty]
bool isPopOpen;

[ObservableProperty]
Student student = new();

[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(RegisterCommand))]
bool canEnableRegisterButton = false;

[RelayCommand]
void OpenPopUp() {
IsPopOpen = true;
}

[RelayCommand]
Task Login() {
return Task.FromResult(0);
}

[RelayCommand(CanExecute = nameof(CanRegisterExecute))]
async Task Register() {

var key = await dataService.AddAsync("Users", Student);
if (key is not null) {
IsBusy = false;
IsVisible = true;
}
}

private bool CanRegisterExecute() {
if (string.IsNullOrEmpty(Student.Email) && string.IsNullOrEmpty(Student.Password)) {
return true;
}

return false;
}
[ObservableProperty]
bool isVisible = true;

[ObservableProperty]
bool isPopOpen;

[ObservableProperty]
Student student = new();

[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(RegisterCommand))]
bool canEnableRegisterButton = false;

[RelayCommand]
void OpenPopUp() {
IsPopOpen = true;
}

[RelayCommand]
Task Login() {
return Task.FromResult(0);
}

[RelayCommand(CanExecute = nameof(CanRegisterExecute))]
async Task Register() {

var key = await dataService.AddAsync("Users", Student);
if (key is not null) {
IsBusy = false;
IsVisible = true;
}
}

private bool CanRegisterExecute() {
if (string.IsNullOrEmpty(Student.Email) && string.IsNullOrEmpty(Student.Password)) {
return true;
}

return false;
}
<Grid
Margin="5"
RowDefinitions="80,80,40">
<inputLayout:SfTextInputLayout
ContainerBackground="White"
ContainerType="Outlined"
Hint="Email"
IsHintAlwaysFloated="True"
IsVisible="{Binding IsVisible}"
OutlineCornerRadius="8">
<Entry Text="{Binding Student.Email}" />
</inputLayout:SfTextInputLayout>

<inputLayout:SfTextInputLayout
Grid.Row="1"
ContainerBackground="White"
ContainerType="Outlined"
Hint="Password"
IsHintAlwaysFloated="True"
IsVisible="{Binding IsVisible}"
OutlineCornerRadius="8">
<Entry Text="{Binding Student.Password}" />
</inputLayout:SfTextInputLayout>

<Button
Grid.Row="3"
Command="{Binding RegisterCommand}"
CornerRadius="8"
IsEnabled="{Binding CanEnableRegisterButton}"
IsVisible="{Binding IsVisible}"
Text="Register" />
</Grid>
<Grid
Margin="5"
RowDefinitions="80,80,40">
<inputLayout:SfTextInputLayout
ContainerBackground="White"
ContainerType="Outlined"
Hint="Email"
IsHintAlwaysFloated="True"
IsVisible="{Binding IsVisible}"
OutlineCornerRadius="8">
<Entry Text="{Binding Student.Email}" />
</inputLayout:SfTextInputLayout>

<inputLayout:SfTextInputLayout
Grid.Row="1"
ContainerBackground="White"
ContainerType="Outlined"
Hint="Password"
IsHintAlwaysFloated="True"
IsVisible="{Binding IsVisible}"
OutlineCornerRadius="8">
<Entry Text="{Binding Student.Password}" />
</inputLayout:SfTextInputLayout>

<Button
Grid.Row="3"
Command="{Binding RegisterCommand}"
CornerRadius="8"
IsEnabled="{Binding CanEnableRegisterButton}"
IsVisible="{Binding IsVisible}"
Text="Register" />
</Grid>
Sergio
Sergio7mo ago
This is not what you said earlier though, I thought you were modifying properties in that viewmodel. You're instead setting properties that are nested in that Student type. That will not work as is
eduardoA
eduardoA7mo ago
why jnot