0

This is driving me crazy, seriously. I have a dialog with an increment decrement pair of buttons that allows the user to select a quantity. The buttons are bound to a Delegate command in the view model, each of this commands take an Action delegate and a Func canExecute. The idea is that the user can select numbers from 1 to 100, so the canExecute parameters are bound to a couple of properties that implement this logic with booleans. When the program starts the default quantity is 1 so the increment button is enabled and the decrement button is disabled. But the idea is that when the number is 2 or more the user can decrease the quantity so the button need to be enabled. The problem is that the decrease button starts disabled and does not update its state when the number changes.
I have tried putting the RaisePropertyChanged method everywhere in the ViewModel, I have tried with the IsEnabled property in the XAML but nothing seems to work. The most weird part is that the increase button that is enabled at the beginning works well and when it reaches 99 stops executing the command but doesn't gets disabled. I will show you the code, any advise will be greatly appreciated. Thanks. This is the XAML:

 <Button x:Name="cmdUp"
              BorderBrush="LightSlateGray"
              Grid.Column="1"
              x:FieldModifier="private"
              FontSize="10"
              Content="▲"
              Width="15"
              Height="17"
              HorizontalAlignment="Right"
              VerticalAlignment="Top"
              Command="{Binding IncreaseQuantityCommand}"/>
      <Button x:Name="cmdDown"
              BorderBrush="LightSlateGray"
              x:FieldModifier="private"
              FontSize="10"
              Grid.Column="1"
              Content="▼"
              Height="17"
              Width="15"
              HorizontalAlignment="Right"
              VerticalAlignment="Bottom"
              IsEnabled="{Binding CanDecrease}"
              Command="{Binding DecreaseQuantityCommand}"  />

This is the Code behind:

/// <summary>
    /// Interaction logic for QuantityModalView.xaml
    /// </summary>
    public partial class QuantityModalView : UserControl
    {
        private QuantityModalViewModel _quantityModalViewModel;
        public QuantityModalView()
        {
            InitializeComponent();
            _quantityModalViewModel = new QuantityModalViewModel();
            DataContext = _quantityModalViewModel;
        }
    }

This is the viewModel:

 public class QuantityModalViewModel : ViewModelBase
    {
        private int _numValue;
        public QuantityModalViewModel()
        {
            _numValue = 1;
            DecreaseQuantityCommand = new DelegateCommand(DecreaseQuantity, () => CanDecrease);
            IncreaseQuantityCommand = new DelegateCommand(IncreaseQuantity, () => CanIncrease);
        }
        public DelegateCommand IncreaseQuantityCommand { get; }
        public DelegateCommand DecreaseQuantityCommand { get; }
        public int NumValue
        {
            get { return _numValue; }
            set
            {
                _numValue = value;
                RaisePropertyChanged();
                RaisePropertyChanged(nameof(CanDecrease));

            }
        }
        public bool CanDecrease => NumValue >= 2;
        public bool CanIncrease => NumValue < 99;



        public void IncreaseQuantity()
        {
            NumValue++;
           
        }
        public void DecreaseQuantity()
        {
            NumValue--;
           
        }
    }
MrG-O-L
  • 35
  • 6

1 Answers1

0

Found it, rookie mistake. I forgot to add the Command.RaiseCanExecuteChanged() method in my property. Amazing what a short stroll around the corner can do! Here's the fix:

public int NumValue
{
    get { return _numValue; }
    set
    {
        _numValue = value;
        RaisePropertyChanged();
        RaisePropertyChanged(nameof(CanDecrease));
        DecreaseQuantityCommand.RaiseCanExecuteChanged();

    }
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
MrG-O-L
  • 35
  • 6