CommandBinding question. How to enable command button

佐手、 提交于 2019-12-07 12:02:58

问题


My code is here>>

public class Player:INotifyPropertyChanging
{
        string addressBar;
        public string Url
        {
            get {

                return addressBar;
            }
            set { addressBar = value; OnPropertyChanged("Url"); }
        }
        public Regex regVillage = new Regex(@"\?doc=\d+&sys=[a-zA-Z0-9]{2}");

        RelayCommand _AddAttackTask;
        public ICommand AddAttackTask
        {
            get {
                if (_AddAttackTask == null)
                {
                    _AddAttackTask = new RelayCommand(param =>
                    {

                    }, param => this.CanAttack);
                }
                return _AddAttackTask;
            }
        }

        public Boolean CanAttack
        {
            get{
                if (Url == null) return false;
                return regVillage.IsMatch(Url);
            }            
        }
}

On the xaml, i have textbox and button. Textbox binded by url, button binded by AddAttackTask. When i change textbox value,Url changed.Main target is When changing url, button bring to enable or disable. But button always disabled.

I'm getting RelayCommand class from WPF Apps With The Model-View-ViewModel Design Pattern

What is wrong on my code?

Please fix my command binding!


回答1:


I found it yourself.

Must call CommandManager.InvalidateRequerySuggested(); function after changing property

public string Url
        {
            get {

                return addressBar;
            }
            set { addressBar = value; OnPropertyChanged("Url"); 
                  CommandManager.InvalidateRequerySuggested();
            }
        }


来源:https://stackoverflow.com/questions/3849265/commandbinding-question-how-to-enable-command-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!