之前的思路一直是在控件的机制,想从控件本身入手。
其实这件事情和控件没有任何关系。
是MVVM中的数据源没有实现通知。
数据源需要做处理,代码参考:
- public class Person : IDataErrorInfo, INotifyPropertyChanged
- {
- private string _name;
- public string Id { get; set; }
- public string Name
- {
- get { return _name; }
- set
- {
- _name = value;
- }
- }
- private string _province = string.Empty;
- [Required(AllowEmptyStrings = false, ErrorMessage = "省份 必填")]
- public string Province
- {
- get
- {
- return _province;
- }
- set
- {
- _province = value;
- OnPropertyChanged("Province");
- }
- }
- protected void OnPropertyChanged(String propertyName)
- {
- PropertyChangedEventArgs e = new PropertyChangedEventArgs(propertyName);
- if (PropertyChanged != null)
- {
- PropertyChanged(this, e);
- }
- }
- public string this[string columnName]
- {
- get
- {
- if (string.IsNullOrEmpty(Province))
- return "省份 必填";
- return string.Empty;
- }
- }
- public string Error { get; private set; }
- public event PropertyChangedEventHandler PropertyChanged;
- }
复制代码 |