【问题标题】:Enable Disable save button during Validation using IDataErrorInfo使用 IDataErrorInfo 在验证期间启用禁用保存按钮
【发布时间】:2011-06-30 06:00:07
【问题描述】:

如何在使用IDataErrorInfo 进行验证时禁用/启用按钮?

我正在使用 MVVM 使用 GalaSoft 灯光框架。在我的模型类中,我实现了IDataErrorInfo 来显示错误消息。

public string this[string columnName]
{
    get
    {
        Result = null;
        if (columnName == "FirstName")
        {
            if (String.IsNullOrEmpty(FirstName))
            {
                Result = "Please enter first name";
            }
        }
        else if (columnName == "LastName")
        {
            if (String.IsNullOrEmpty(LastName))
            {
                Result = "Please enter last name";
            }
        }

        else if (columnName == "Address")
        {
            if (String.IsNullOrEmpty(Address))
            {
                Result = "Please enter Address";
            }
        }

        else if (columnName == "City")
        {
            if (String.IsNullOrEmpty(City))
            {
                Result = "Please enter city";
            }
        }

        else if (columnName == "State")
        {
            if (State == "Select")
            {
                Result = "Please select state";
            }
        }

        else if (columnName == "Zip")
        {
            if (String.IsNullOrEmpty(Zip))
            {
                Result = "Please enter zip";

            }
            else if (Zip.Length < 6)
            {
                Result = "Zip's length has to be at least 6 digits!";

            }
            else
            {
                bool zipNumber = Regex.IsMatch(Zip, @"^[0-9]*$");

                if (zipNumber == false)
                {
                    Result = "Please enter only digits in zip";


                }
            }
        }
        else if (columnName == "IsValid")
        {
            Result = true.ToString();
        }

        return Result;

    }
}

截图:http://i.stack.imgur.com/kwEI8.jpg

如何禁用/启用保存按钮。请推荐?

谢谢

【问题讨论】:

    标签: c# wpf mvvm light


    【解决方案1】:

    Josh Smith Way这样做是在Model中创建如下方法:

    static readonly string[] ValidatedProperties =
    {
        "Foo",
        "Bar"
    };
    
    /// <summary>
    /// Returns true if this object has no validation errors.
    /// </summary>
    public bool IsValid
    {
        get
        {
            foreach (string property in ValidatedProperties)
            {
    
                if (GetValidationError(property) != null) // there is an error
                    return false;
            }
    
            return true;
        }
    }
    
    private string GetValidationError(string propertyName)
    {
        string error = null;
    
        switch (propertyName)
        {
            case "Foo":
                error = this.ValidateFoo();
                break;
    
            case "Bar":
                error = this.ValidateBar();
                break;
    
            default:
                error = null;
                throw new Exception("Unexpected property being validated on Service");
        }
    
        return error;
    }
    

    ViewModel 然后包含一个CanSave 属性,它读取模型上的IsValid 属性:

    /// <summary>
    /// Checks if all parameters on the Model are valid and ready to be saved
    /// </summary>
    protected bool CanSave
    {
        get
        {
            return modelOfThisVM.IsValid;
        }
    }
    

    最后,如果使用RelayCommand,可以将命令的谓词设置为CanSave属性,View会自动启用或禁用按钮。在 ViewModel 中:

    /// <summary>
    /// Saves changes Command
    /// </summary>
    public ICommand SaveCommand
    {
        get
        {
            if (_saveCommand == null)
                _saveCommand = new RelayCommand(param => this.SaveChanges(), param => this.CanSave);
    
            return _saveCommand;
        }
    }
    

    在视图中:

    <Button Content="Save" Command="{Binding Path=SaveCommand}"/>
    

    就是这样!

    PS:如果您还没有阅读 Josh Smith 的文章,它将改变您的生活。

    【讨论】:

    • 这不是将验证逻辑放入模型中的好方法,因为验证逻辑可能会在不同的场景中发生变化。
    • 为简单起见,它可以只是public bool IsValid =&gt; ValidatedProperties.All(p =&gt; GetValidationError(p) == null);
    【解决方案2】:

    您可以添加布尔属性 CanSave 并将其设置在验证方法的末尾。将按钮中的 IsEnabled 绑定到 IsValid。 是这样的:

    public bool CanSave
    {
      get{ return canSave; }
      set{ canSave = value; RaisePropertyChanged( "CanSave" ); }
    }
    private bool canSave;
    
    public string this[string columnName]
    {
      //....
      CanSave = Result == String.Empty;
    }
    
    //xaml
    <Button IsEnabled={Binding Path=CanSave}>Save</Button>
    

    【讨论】:

    • 如果你使用IDataErrorInfo,这实际上比接受的答案要好得多如果你像我现在这样使用验证规则属性,你可能不得不接受接受的答案:)
    • 如果有多个验证,则无法正常工作...每个属性都会调用 this[string columnName]。因此,如果 prop1 无效,而 prop2 有效,则 CanSave 设置为 true。
    • @PetervanKekem 该部分在//.... 中:每次调用该函数时,都必须根据之前的结果重新计算Result..
    • 是的,这是可能的。但是,我认为这太冒险/太复杂了。我选择实施@PieterMüller 的解决方案。
    【解决方案3】:

    这是我结合使用 IDataErrorInfo 接口、ValidationErrors Dictionary 和 MVVM-Light 消息系统的方法。直截了当,像魅力一样工作:

    模型类

    public Dictionary<string, string> ValidationErrors = new Dictionary<string, string>();
    
    public string this[string columnName]
    {
      get
       {
         // Remove Property error from ValidationErrors prior to any validation
         ValidationErrors.Remove(propertyName);
         //----------------------------------------
         string Result = null;
         if (columnName == "FirstName")
         {
            if (String.IsNullOrEmpty(FirstName))
            {
               // Add Property error to ValidationErrors Dic
               ValidationErrors[propertyName] = Result = "Please enter first name";
               //----------------------------------------
            }
         }
         else if (columnName == "LastName")
         {
            if (String.IsNullOrEmpty(LastName))
            {
              // Add Property error to ValidationErrors Dic
              ValidationErrors[propertyName] = Result = "Please enter last name";
              //----------------------------------------
            }
         }
    
        // Send MVVM-Light message and receive it in the Code Behind or VM
        Messenger.Default.Send<PersonInfoMsg>(new PersonInfoMsg());
        //----------------------------------------
        return Result;
      }
    }
    

    查看背后的代码

     public partial class PersonInfoView : UserControl
      {
        public PersonInfoView()
        {
            InitializeComponent();
            Messenger.Default.Register<PersonInfoMsg>(this, OnErrorMsg);
        }
    
        private void OnErrorMsg(PersonInfoMsg)
        {
            // In case of DataGrid validation
            foreach (PersonInfoModel p in GridName.ItemsSource)
            {
                if (p.ValidationErrors.Count == 0)
                    SaveBtn.IsEnabled = true;
                else
                    SaveBtn.IsEnabled = false;
            }
         }
      }
    

    【讨论】:

      猜你喜欢
      • 2021-03-25
      • 1970-01-01
      • 2013-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-07
      • 1970-01-01
      相关资源
      最近更新 更多