[英]WPF field validation
嗨,我是 C# 新手,我創建了一個 WPF 表單,該表單接收 csv 並將其保存在 db 中。 現在我想知道是否有一個Validation Library
可以用來使這些字段檢查更容易? 就像我想實現一個必填字段或其他東西,如果不需要該字段,它會拋出錯誤。 我怎樣才能做到這一點? 我已經閱讀了一些類似這樣的代碼
[Required] // Not sure if this is the correct format
public string Name()
{
get;set;
}
所以基本上我在考慮一種中間件類型的驗證,我只是將[Required]
屬性放在模型類中,它會為我做驗證。 有這樣的事情嗎?
更新:我所說的驗證是指當我選擇一個 csv 文件后,它將讀取每行/列,現在每列都將像 ex 一樣進行驗證。 對於column[0]
(假設這是 Name 字段),那么當讀取此字段並將其分配給變量時,應在繼續分配給變量之前對其進行驗證。 不確定這是否可能更多,它是這樣的
string[] content = row.Split('|');
Customer customer = new Customer(content);
Lis<KeyValuePair<string, string>> rowList = Converter.ToKeyValuePair(customer);
這是Customer
類的代碼
class Converter
{
public static List<KeyValuePair<string,string>> ToKeyValuePair(Customer customer)
{
List<KeyValuePair<string, string>> rowList = new List<keyValuePari<string, string>>();
rowList.Add(new KeyValuePair<string, string>('name', customer.Name));
}
return rowList;
}
// 類客戶
class Customer
{
private string _name;
public Customer(string[] content)
{
this.Name = content[0];
}
[Required] // Maybe some auto validation here where when the property is accessed it should be validated immediately
public string Name
{
get { return _name; }
set {
// I'm thinking of some validation here?
_name = value;
}
}
}
正如您在我的示例中所看到的,我希望每當訪問this.Name = content[0]
類的屬性時,它都會自動驗證內容是否具有值
沒有內置的“驗證庫”。 你應該在你的類中實現INotifyDataErrorInfo
接口:
public class Model
{
[Required(ErrorMessage = "You must enter a username.")]
[StringLength(10, MinimumLength = 4,
ErrorMessage = "The username must be between 4 and 10 characters long")]
[RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "The username must only contain letters (a-z, A-Z).")]
public string Username { get; set; }
[Required(ErrorMessage = "You must enter a name.")]
public string Name { get; set; }
}
public class ViewModel : INotifyDataErrorInfo
{
private readonly Dictionary<string, ICollection<string>>
_validationErrors = new Dictionary<string, ICollection<string>>();
private readonly Model _user = new Model();
public string Username
{
get { return _user.Username; }
set
{
_user.Username = value;
ValidateModelProperty(value, "Username");
}
}
public string Name
{
get { return _user.Name; }
set
{
_user.Name = value;
ValidateModelProperty(value, "Name");
}
}
protected void ValidateModelProperty(object value, string propertyName)
{
if (_validationErrors.ContainsKey(propertyName))
_validationErrors.Remove(propertyName);
ICollection<ValidationResult> validationResults = new List<ValidationResult>();
ValidationContext validationContext =
new ValidationContext(_user, null, null) { MemberName = propertyName };
if (!Validator.TryValidateProperty(value, validationContext, validationResults))
{
_validationErrors.Add(propertyName, new List<string>());
foreach (ValidationResult validationResult in validationResults)
{
_validationErrors[propertyName].Add(validationResult.ErrorMessage);
}
}
RaiseErrorsChanged(propertyName);
}
...
}
此博客文章中描述了 WPF 中的完整數據驗證過程,包括如何實現此接口和使用數據注釋驗證數據的示例。 請通讀一遍,如果您遇到更具體的問題,請隨時提出另一個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.