diff --git a/Common.Wpf.csproj b/Common.Wpf.csproj index 09abecf..c71e7af 100644 --- a/Common.Wpf.csproj +++ b/Common.Wpf.csproj @@ -154,7 +154,7 @@ - + @@ -181,7 +181,7 @@ - + TextList.xaml @@ -190,6 +190,7 @@ ImageButton.xaml + @@ -221,7 +222,7 @@ MSBuild:Compile Designer - + Designer MSBuild:Compile diff --git a/Extensions/WindowExtensions.cs b/Extensions/WindowExtensions.cs index 5c5a517..206be9b 100644 --- a/Extensions/WindowExtensions.cs +++ b/Extensions/WindowExtensions.cs @@ -102,7 +102,7 @@ namespace Common.Wpf.Extensions public static void ClearAllValidationErrors(this DependencyObject window, IEnumerable bindingExpressions) { foreach (var expression in bindingExpressions) - Validation.ClearInvalid(expression.BindingExpression); + System.Windows.Controls.Validation.ClearInvalid(expression.BindingExpression); } public static bool Validate(this Window window) diff --git a/EnumToResourceConverter.cs b/Resource/EnumToResourceConverter.cs similarity index 96% rename from EnumToResourceConverter.cs rename to Resource/EnumToResourceConverter.cs index 3104fd3..1112791 100644 --- a/EnumToResourceConverter.cs +++ b/Resource/EnumToResourceConverter.cs @@ -3,7 +3,7 @@ using System.Globalization; using System.Resources; using System.Windows.Data; -namespace Common.Wpf +namespace Common.Wpf.Resource { public class EnumToResourceConverter : IValueConverter { diff --git a/RepositionPopupBehavior.cs b/Validation/RepositionPopupBehavior.cs similarity index 99% rename from RepositionPopupBehavior.cs rename to Validation/RepositionPopupBehavior.cs index bf6352c..8b0a906 100644 --- a/RepositionPopupBehavior.cs +++ b/Validation/RepositionPopupBehavior.cs @@ -3,7 +3,7 @@ using System.Windows; using System.Windows.Controls.Primitives; using System.Windows.Interactivity; -namespace Common.Wpf +namespace Common.Wpf.Validation { /// /// Defines the reposition behavior of a control when the window to which it is attached is moved or resized. diff --git a/Validation/ValidationModelBase.cs b/Validation/ValidationModelBase.cs new file mode 100644 index 0000000..56e58e3 --- /dev/null +++ b/Validation/ValidationModelBase.cs @@ -0,0 +1,86 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.ComponentModel; +using System.Runtime.CompilerServices; + +namespace Common.Wpf.Validation +{ + public class ValidationModelBase : INotifyPropertyChanged, INotifyDataErrorInfo + { + #region Property changed + public event PropertyChangedEventHandler PropertyChanged = delegate { }; + + protected void NotifyPropertyChanged(string propertyName) + { + PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); + } + #endregion + + #region Notify data error + private readonly Dictionary> _errors = new Dictionary>(); + public event EventHandler ErrorsChanged = delegate { }; + + // get errors by property + public IEnumerable GetErrors(string propertyName) + { + if (_errors.ContainsKey(propertyName)) + return _errors[propertyName]; + return null; + } + + // has errors + public bool HasErrors + { + get { return (_errors.Count > 0); } + } + + // object is valid + public bool IsValid + { + get { return !HasErrors; } + + } + + public void AddError(string error, [CallerMemberName] string propertyName = null) + { + if (propertyName == null) + return; + + // Add error to list + _errors[propertyName] = new List { error }; + NotifyErrorsChanged(propertyName); + } + + public void RemoveError([CallerMemberName] string propertyName = null) + { + if (propertyName == null) + return; + + // remove error + if (_errors.ContainsKey(propertyName)) + _errors.Remove(propertyName); + + NotifyErrorsChanged(propertyName); + } + + private void NotifyErrorsChanged(string propertyName) + { + ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName)); + } + + #endregion + + protected void SetProperty(ref T propertyValue, T newValue, [CallerMemberName] string propertyName = null) + { + if (Equals(propertyValue, newValue)) + return; + + propertyValue = newValue; + NotifyPropertyChanged(propertyName); + NotifyErrorsChanged(propertyName); + } + + + } +} diff --git a/ValidationResources.xaml b/Validation/ValidationResources.xaml similarity index 96% rename from ValidationResources.xaml rename to Validation/ValidationResources.xaml index ea689c7..77d3870 100644 --- a/ValidationResources.xaml +++ b/Validation/ValidationResources.xaml @@ -1,7 +1,7 @@  + xmlns:validation="clr-namespace:Common.Wpf.Validation"> @@ -39,7 +39,7 @@ PlacementRectangle="10,-1,0,0"> - +