0

I have a textbox bound to an nullable integer in my model class. My CanExecute method works as expected. I want the button routed by the command to be disabled if the textbox value is 0. However, it will not disable when I completely cleare out the text box. How do I fix this validation issue?

xaml:

<TextBox Text="{Binding GameStats.TimeoutMinutes, UpdateSourceTrigger=PropertyChanged}"></TextBox>

<Button Command="{Binding CustomizeTimeoutTimeCmd}"/>

Model property:

private int? timeoutMinutes = 0;
public int? TimeoutMinutes {
   get { return timeoutMinutes; }
   set {
      timeoutMinutes = value;
      OnPropertyChanged("TimeoutMinutes");
   }
}

Command CanExecute method:

public bool CanExecute(object parameter) {
    if (scoreViewModel.GameStats.TimeoutMinutes == 0 || scoreViewModel.GameStats.TimeoutMinutes == null) {
         return false;
     } else {
         return true;
     }      
}
nikotromus
  • 1,015
  • 16
  • 36
  • 1
    The setter for the property doesn't fire when I clear out the textbox. So, if I have a value of 105, it will fire when I delete the 5 and when I delete the 0, but when I delete the 1 to clear out the textbox, the setter does not fire so it can never equal null for the evaluation. – nikotromus Aug 21 '18 at 12:16
  • you need something like CustomizeTimeoutTimeCmd.RaiseCanExecuteChanged() – Gilad Aug 21 '18 at 12:17
  • @mjwills That article led me to this article, and I found the solution. Thank you!!!!!!!!!!!!!!!!!!!!! https://stackoverflow.com/questions/1895453/set-value-to-null-in-wpf-binding/1895482#1895482 – nikotromus Aug 21 '18 at 12:24

0 Answers0