Using WPF Validation rules and the disabling of a 'Save' button

前端 未结 10 2051
眼角桃花
眼角桃花 2020-11-28 09:54

I have a page where a few textboxes cannot be empty before clicking a Save button.


                            


        
10条回答
  •  难免孤独
    2020-11-28 10:21

    just inhert your ViewModel from System.ComponentModel.IDataErrorInfo for Validate and from INotifyPropertyChanged to notify button

    make property:

        public bool IsValid
        {
            get
            {
                if (this.FloorPlanName.IsEmpty())
                    return false;
                return true;
            }
        }
    

    in xaml, connect it to button

    in the IDataErrorInfo overrides, notify btutton

    public string this[string columnName]{
            get
            {
                switch (columnName)
                {
                    case "FloorPlanName":
                        if (this.FloorPlanName.IsEmpty())
                        {
                            OnPropertyChanged("IsValid");
                            return "Floor plan name cant be empty";
                        }
                        break;
                }
            }
    }
    

提交回复
热议问题