-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a BaseClass which support:
- INotifyPropertyChanged - INotifyDataErrorInfo
- Loading branch information
Showing
1 changed file
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace MahApps.Demo.Core | ||
{ | ||
public class BaseClass : INotifyPropertyChanged, INotifyDataErrorInfo | ||
{ | ||
#region INotifyPropertyChanged | ||
|
||
// This event tells the UI to update | ||
public event PropertyChangedEventHandler PropertyChanged; | ||
|
||
public void RaisePropertyChanged(string PropertyName) | ||
{ | ||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName)); | ||
} | ||
|
||
#endregion INotifyPropertyChanged | ||
|
||
#region INotifyDataErrorInfo | ||
|
||
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged; | ||
|
||
public bool HasErrors => _errorsByPropertyName.Count > 0; | ||
|
||
private readonly Dictionary<string, List<string>> _errorsByPropertyName = new Dictionary<string, List<string>>(); | ||
|
||
public IEnumerable GetErrors(string propertyName) | ||
{ | ||
if (string.IsNullOrEmpty(propertyName)) | ||
{ | ||
return null; | ||
} | ||
else | ||
{ | ||
return _errorsByPropertyName.ContainsKey(propertyName) | ||
? _errorsByPropertyName[propertyName] | ||
: null; | ||
} | ||
} | ||
|
||
public bool GetHasError(string PropertyName) | ||
{ | ||
return _errorsByPropertyName.ContainsKey(PropertyName); | ||
} | ||
|
||
public void AddError(string propertyName, string error) | ||
{ | ||
if (!_errorsByPropertyName.ContainsKey(propertyName)) | ||
_errorsByPropertyName[propertyName] = new List<string>(); | ||
|
||
if (!_errorsByPropertyName[propertyName].Contains(error)) | ||
{ | ||
_errorsByPropertyName[propertyName].Add(error); | ||
OnErrorsChanged(propertyName); | ||
} | ||
} | ||
|
||
public void ClearErrors(string propertyName) | ||
{ | ||
if (_errorsByPropertyName.ContainsKey(propertyName)) | ||
{ | ||
_errorsByPropertyName.Remove(propertyName); | ||
OnErrorsChanged(propertyName); | ||
} | ||
} | ||
|
||
public void OnErrorsChanged(string propertyName) | ||
{ | ||
ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(propertyName)); | ||
} | ||
|
||
#endregion INotifyDataErrorInfo | ||
} | ||
} |