<StackLayout Grid.Row="2" Orientation="Horizontal" Margin="10,5">
<Entry Placeholder="Task name" Text="{Binding TaskName, Mode=TwoWay}"
HorizontalOptions="FillAndExpand" />
<Button Text="+" FontSize="Large" FontAttributes="Bold" WidthRequest="70"
BackgroundColor="{StaticResource ContrastColor}"
TextColor="{StaticResource Backgorund}"
Command="{Binding AddTaskCommand}" />
</StackLayout>
string taskName;
public string TaskName
{
get => taskName;
set
{
base.SetProperty(ref taskName, value);
((Command)AddTaskCommand).ChangeCanExecute();
}
}
public ICommand AddTaskCommand { get => new Command(
execute: () => TaskName = string.Empty,
canExecute: () => true);
}
Let's add some logic into the can execute func
string taskName;
public string TaskName
{
get => taskName;
set
{
base.SetProperty(ref taskName, value);
((Command)AddTaskCommand).ChangeCanExecute();
}
}
public ICommand AddTaskCommand
{
get => new Command(
execute: () => TaskName = string.Empty,
canExecute: () => !String.IsNullOrEmpty(TaskName));
}
string taskName;
public string TaskName {
get => taskName;
set {
base.SetProperty(ref taskName, value);
((Command)AddTaskCommand).ChangeCanExecute();
}
}
public ICommand AddTaskCommand { get; private set; }
public ProjectPageViewModel()
{
AddTaskCommand = new Command(
execute: () => TaskName = string.Empty,
canExecute: () => !String.IsNullOrEmpty(TaskName));
}
string taskName;
public string TaskName
{
get => taskName;
set {
base.SetProperty(ref taskName, value);
((Command)AddTaskCommand).ChangeCanExecute();
}
}
ICommand addTaskCommand = null;
public ICommand AddTaskCommand {
get {
this.addTaskCommand = this.addTaskCommand ?? new Command(
execute: () => TaskName = string.Empty,
canExecute: () => !String.IsNullOrEmpty(TaskName));
return addTaskCommand;
}
}