I've got a TextBox for the user to enter a string, and an "Add" button to perform some tasks with the value. I added a CanExecute method to the DelegateCommand so that the button would only work when there was text in the box.
However, it's disabled all the time - even when the condition is true. Typing in the box does not enable the button.
<TextBox Text="{Binding BuildEntryText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<Button Command="{Binding AddBuildCommand}" Content="Add" />
this.AddBuildCommand = new DelegateCommand(() => this.AddBuild(), () => this.CanAddBuild());
private bool CanAddBuild()
{
if (this.BuildEntryText != null)
{
return true;
}
else
{
return false;
}
}
I've had this happen before (not always with the same types of controls), and ordinarily I would just remove the CanExecute and change it to an "if" check inside the action method. This time, though, I actually have to get it working and grey the button out the "proper" way.
Any insight would be greatly appreciated. Thanks in advance.
UPDATE:
I've added RaiseCanExecuteChanged to the set portion of the BuildEntryText property. Now the button starts disabled and becomes enabled when text is first typed. However, it remains enabled even after the text is deleted or the box is cleared by the add function.
public string BuildEntryText
{
get
{
return this.buildEntryText;
}
set
{
this.SetProperty<string>(ref this.buildEntryText, value);
this.AddBuildCommand.RaiseCanExecuteChanged();
}
}