首页 > 解决方案 > 在 ViewModel 中更新其绑定后,按钮未启用

问题描述

这让我发疯,说真的。我有一个对话框,其中包含一对递增递减按钮,允许用户选择数量。这些按钮绑定到视图模型中的一个 Delegate 命令,每个命令都接受一个 Action 委托和一个 Func canExecute。这个想法是用户可以选择从 1 到 100 的数字,因此 canExecute 参数绑定到几个使用布尔值实现此逻辑的属性。当程序启动时,默认数量为 1,因此启用增量按钮并禁用减量按钮。但想法是,当数字为 2 或更多时,用户可以减少数量,因此需要启用按钮。问题是减少按钮开始禁用并且在数字更改时不会更新其状态。
我尝试将 RaisePropertyChanged 方法放在 ViewModel 中的任何位置,我尝试使用 XAML 中的 IsEnabled 属性,但似乎没有任何效果。最奇怪的是,一开始启用的增加按钮效果很好,当它达到 99 时停止执行命令但没有被禁用。我将向您展示代码,任何建议将不胜感激。谢谢。这是 XAML:

 <Button x:Name="cmdUp"
              BorderBrush="LightSlateGray"
              Grid.Column="1"
              x:FieldModifier="private"
              FontSize="10"
              Content="▲&quot;
              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="▼&quot;
              Height="17"
              Width="15"
              HorizontalAlignment="Right"
              VerticalAlignment="Bottom"
              IsEnabled="{Binding CanDecrease}"
              Command="{Binding DecreaseQuantityCommand}"  />

这是背后的代码:

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

这是视图模型:

 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--;
           
        }
    }

标签: c#wpfmvvmbinding

解决方案


找到了,菜鸟的错误。我忘记在我的属性中添加 Command.RaiseCanExecuteChanged() 方法。令人惊讶的是,在拐角处短暂漫步可以做什么!这是修复:

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

    }
}

推荐阅读