Skip to content

Commit

Permalink
Implement a BaseClass which support:
Browse files Browse the repository at this point in the history
- INotifyPropertyChanged
- INotifyDataErrorInfo
  • Loading branch information
timunie authored and punker76 committed Aug 12, 2020
1 parent a98905b commit 40e3e4d
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src/MahApps.Metro.Demo_v2/Core/BaseClass.cs
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
}
}

0 comments on commit 40e3e4d

Please sign in to comment.