Tuesday, August 14, 2007

WPF expanders and error providers

Task: Add a red error box around a textbox when it fails validation.

Problem: Expanders in WPF hide the contents making them not visible when collapsed. The AdornedElementPlaceholder is rendered on its own layer that is NOT contained within the Expander. This means when you collapse the expander while you have any kind of error shown (even if you customise etc) then the textbox disappears and the error does not.

Workaround: The best I could do was use my own ControlTemplate, hide the AdornedElementPlaceholder within a DockPanel and then use a trigger to change the textbox's border to red, indicating an error. Because I'm changing the actual textbox there is no problems with it being hidden when required by the expander.

Update: Originally I posted XAML here but blogger thought it was html so will have to figure out how to post XAML. doh!
There was a code post converter somewhere... hmmm that might do the trick

Of course if anyone has a better solution (some way to bind the Visibility of the dockpanel to the IsVisible of the textbox for example? Tried it... wouldnt bind, different parents?) please let me know.


Varsha Mahadevan (MSFT) said...

Try adding an AdornerDecorator around the TextBox.


The schematic representation shown above is due to the inability to add xaml markup to this comment.

Lyynx said...

Sweet! Thanks for that, I just tried it and it works!

Such a simple fix. Easy when you know how. :)

Erick T. said...

I need the code.... can you send to me ? Thks!