You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I had problems with ValidationErrors not being removed from the Grid. Someone shared a workaround on Stackoverflow, which fixes the underlying issue in the WPF DataGrid using Reflection:
@tom-englert I think this is a useful addition to the library, because a lot of people are going to have the same issues when using Validations.
Here is what I have used:
usingSystem.Linq;usingSystem.Reflection;usingSystem.Windows.Controls.Primitives;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Media;usingSystem.Windows;namespaceWideWorldImporters.Wpf.Extensions{/// <summary>/// The DataGrid doesn't remove the Validation Error notification, because of a Binding problem. This /// Bugfix has been shared at: https://stackoverflow.com/a/61613772/513875/// </summary>publicstaticclassDataGridExtensions{/// <summary>/// Identifies the FixBindingGroupValidationErrorsFor attached property. /// </summary>publicstaticreadonlyDependencyPropertyFixBindingGroupValidationErrorsForProperty=DependencyProperty.RegisterAttached("FixBindingGroupValidationErrorsFor",typeof(DependencyObject),typeof(DataGridExtensions),newPropertyMetadata(null,newPropertyChangedCallback(OnFixBindingGroupValidationErrorsForChanged)));/// <summary>/// Gets the value of the FixBindingGroupValidationErrorsFor property/// </summary>publicstaticDependencyObjectGetFixBindingGroupValidationErrorsFor(DependencyObjectobj){return(DependencyObject)obj.GetValue(FixBindingGroupValidationErrorsForProperty);}/// <summary>/// Sets the value of the FixBindingGroupValidationErrorsFor property/// </summary>publicstaticvoidSetFixBindingGroupValidationErrorsFor(DependencyObjectobj,DependencyObjectvalue){obj.SetValue(FixBindingGroupValidationErrorsForProperty,value);}/// <summary>/// Handles property changed event for the FixBindingGroupValidationErrorsFor property./// </summary>privatestaticvoidOnFixBindingGroupValidationErrorsForChanged(DependencyObjectd,DependencyPropertyChangedEventArgse){DependencyObjectoldobj=(DependencyObject)e.OldValue;if(oldobj==null){return;}BindingGroup?group=FindBindingGroup(d);if(group==null){return;}varleftOverErrors=group.ValidationErrors!=null?Validation.GetErrors(group.Owner).Except(group.ValidationErrors).ToArray():Validation.GetErrors(group.Owner).ToArray();foreach(varerrorinleftOverErrors){//HINT: BindingExpressionBase.Detach() removes the reference to BindingGroup, before ValidationErrors are removed.if(error.BindingInErrorisBindingExpressionBasebinding&&(binding.Target==null||TreeHelper.IsDescendantOf(binding.Target,oldobj))&&binding.BindingGroup==null&&(binding.ValidationErrors==null||binding.ValidationErrors.Count==0||!binding.ValidationErrors.Contains(error))){typeof(Validation).GetMethod("RemoveValidationError",BindingFlags.Static|BindingFlags.NonPublic)?.Invoke(null,newobject[]{error,group.Owner,group.NotifyOnValidationError});}}}privatestaticBindingGroup?FindBindingGroup(DependencyObjectobj){do{if(objisFrameworkElementfe){returnfe.BindingGroup;}if(objisFrameworkContentElementfce){returnfce.BindingGroup;}obj=LogicalTreeHelper.GetParent(obj);}while(obj!=null);returnnull;}privatestaticclassTreeHelper{privatestaticDependencyObject?GetParent(DependencyObjectelement,boolrecurseIntoPopup){if(recurseIntoPopup){// Case 126732 : To correctly detect parent of a popup we must do that exception casePopup?popup=elementasPopup;if((popup!=null)&&(popup.PlacementTarget!=null)){returnpopup.PlacementTarget;}}Visual?visual=elementasVisual;DependencyObject?parent=(visual!=null)?VisualTreeHelper.GetParent(visual):null;if(parent==null){// No Visual parent. Check in the logical tree.parent=LogicalTreeHelper.GetParent(element);if(parent==null){FrameworkElement?fe=elementasFrameworkElement;if(fe!=null){parent=fe.TemplatedParent;}else{FrameworkContentElement?fce=elementasFrameworkContentElement;if(fce!=null){parent=fce.TemplatedParent;}}}}returnparent;}publicstaticboolIsDescendantOf(DependencyObjectelement,DependencyObjectparent){returnIsDescendantOf(element,parent,true);}publicstaticboolIsDescendantOf(DependencyObject?element,DependencyObjectparent,boolrecurseIntoPopup){while(element!=null){if(element==parent){returntrue;}element=GetParent(element,recurseIntoPopup);}returnfalse;}}}}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I had problems with ValidationErrors not being removed from the Grid. Someone shared a workaround on Stackoverflow, which fixes the underlying issue in the WPF DataGrid using Reflection:
@tom-englert I think this is a useful addition to the library, because a lot of people are going to have the same issues when using Validations.
Here is what I have used:
Beta Was this translation helpful? Give feedback.
All reactions