2011-06-30 30 views
8

Làm thế nào để tắt/bật nút khi thực hiện xác thực bằng cách sử dụng IDataErrorInfo?Bật Tắt nút lưu trong khi Xác thực bằng IDataErrorInfo

Tôi đang sử dụng MVVM bằng GalaSoft Light Framework. Trong lớp Model của tôi, tôi đã thực hiện IDataErrorInfo để hiển thị các thông báo lỗi.

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; 

    } 
} 

Ảnh chụp màn hình: http://i.stack.imgur.com/kwEI8.jpg

Làm thế nào để tắt/bật nút lưu. Vui lòng đề nghị?

Cảm ơn

Trả lời

17

Các Josh Smith Way để làm điều này là để tạo ra các phương pháp sau đây trong 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 sau đó chứa một tài sản CanSave mà đọc IsValid tài sản trên Model:

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

Cuối cùng, nếu bạn đang sử dụng RelayCommand, bạn có thể đặt predica te của lệnh đến thuộc tính CanSave và Chế độ xem sẽ tự động bật hoặc tắt nút. Trong ViewModel:

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

     return _saveCommand; 
    } 
} 

Và trong Xem:

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

Và đó là nó!

PS: Nếu bạn chưa đọc bài viết của Josh Smith, nó sẽ thay đổi cuộc sống của bạn.

+0

đó không phải là một cách tiếp cận tốt để đặt logic xác nhận trong Mô hình của bạn, bởi vì logic xác thực có thể thay đổi trong kịch bản khác nhau. –

+0

Để đơn giản, nó có thể chỉ là 'công bool IsValid => ValidatedProperties.All (p => GetValidationError (p) == null);'. – dee

8

bạn có thể thêm một thuộc tính boolean CanSave và đặt ở cuối phương pháp tính giá trị của bạn. Liên kết IsEnabled từ nút của bạn với IsValid. somthing như thế này:

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> 
+0

Đây là thực sự tốt hơn nhiều so với câu trả lời chấp nhận nếu bạn sử dụng 'IDataErrorInfo' nếu bạn chỉ cần sử dụng các thuộc tính quy tắc hợp lệ như tôi ngay bây giờ, bạn có thể phải đi với câu trả lời được chấp nhận :) – GONeale

+5

Không hoạt động nếu có nhiều xác thực ... this [str ing columnName] được gọi cho mỗi thuộc tính. Vì vậy, nếu một prop1 là không hợp lệ, và prop2 là hợp lệ, CanSave được đặt thành true. –

+0

@PetervanKekem phần đó nằm trong '// ....': mọi lúc hàm được gọi, 'Kết quả' phải được tính lại dựa trên kết quả trước đó .. – stijn

1

Đây là cách tôi thực hiện bằng cách sử dụng kết hợp giữa giao diện IDataErrorInfo, Từ điển ValidationErrors và Hệ thống nhắn tin MVVM-Light. Thẳng về phía trước và hoạt động giống như sự quyến rũ:

Mẫu Lớp

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

Xem Mã Đằng sau

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; 
     } 
    } 
    } 
Các vấn đề liên quan