Apparent Bug in WarningAndErrorValidator

Topics: Issues / bugs
Nov 5, 2012 at 1:26 PM

The WarningAndErrorValidator, in its Initialize() method, searches for an InfoBarMessageControl up its logical tree, and if it finds one, it causes the InfoBarMessageControl to subscribe to the WarningAndErrorValidator.  I believe this should be modified to search through the visual and logical tree (per this Josh Smith article: )

The problem is that every ControlTemplate and DataTemplate contain their own LogicalTree.  So, if a WarningAndErrorValidator is placed inside a template, and the InfoBarMessageControl is outside of that template, walking up the Logical Tree alone will not find the InfoBarMessageControl.  

One example that I have run into is when you have a ListBox on a DataWindow, and the ListBox.ItemTemplate contains a Catel.UserControl.  The WarningAndErrorValidator that is created automatically for the UserControl will not find the InfoBarMessageControl on the DataWindow.  This is because the method FindLogicalAncestor (in DependencyObjectExtensions) will walk up the Logical Tree until it gets to the top element of the DataTemplate, then the next GetParent() call will return null, so the method returns null.

Nov 5, 2012 at 2:51 PM

Incidentally, it would be nice if InfoBarMessageControl.SubscribeWarningAndErrorValidator were Public instead of Internal, so it would be possible to manually link an InfoBarMessageControl to a WarningAndErrorValidator that was not directly in its Logical or Visual Tree.

Nov 7, 2012 at 8:28 AM

They are now public. I will look into your issue probably tonight.

Nov 7, 2012 at 11:44 AM

Thanks, Geert.  Have you posted these changes yet?  The last source code version I see on the Source Code page is from last Friday.  If not, I can wait - just wondering if I am missing it somewhere.

Nov 7, 2012 at 5:09 PM

I am not sure about your first question. Walking both the visual and logical tree can be very expensive. However, Catel already provides a good method for this (DependencyObjectExtensions.FindLogicalOrVisualAncestor). I will use that one and release a new version via nuget tonight.

Nov 7, 2012 at 5:25 PM

Are you saying that you will have the WarningandErrorValidator use FindLogicalOrVisualAncestor instead of FindLogicalAncestor when searching for a InfoBarMessageControl?  If so, that would be perfect.  You said "Walking both the visual and logical tree can be very expensive."  So I am not sure how you propose to use this method.

Nov 7, 2012 at 5:26 PM

Yes, I will use FindLogicalOrVisualAncestor instead. If I think it is too much of a performance hit, we can also revert the change.